Javascript click not working as desired

gomesh munda

I am trying to implement something like what is pictured below; when Divs looking like buttons are clicked, it would show details about them. I am using inline Javascript as well as an external JavaScript file. My problem is that when I click on the first div, it shows detailed information about it. But when I click the other divs nothing happens.

My HTML code is:

<html>
<head>
<title>Crash Test</title>
    <link href="css/style.css" type="text/css" rel="stylesheet">
    <script src="javascript/functions.js" type="text/javascript"></script>

    </head>
    <body>
        <fieldset>
            <legend>Mobile Phones</legend>
            <table width="100%">
                <tbody>
                    <tr>
                        <td>
                            <script language="javascript">
                                var secArray = new Array();
                                secArray = ["Asus","HTC","Apple","Samsung","Nokia"];

                                for(var secCnt=1;secCnt<=secArray.length;secCnt++){
                                    document.write('<div class="allSections currentSectionSelected" id="s">');
                                    document.write('<div href="javascript:void(0);" class="tooltip tooltipSelected">');
                                    document.write('<div style="text-overflow:ellipsis;width:92%;overflow:hidden;white-space:nowrap; padding:5px;font-weight: bold;" onclick="javascript:setSecName('+secCnt+',secArray);">');
                                    document.write(secArray[secCnt-1]);
                                    document.write('</div>');
                                    document.write('</div>');
                                    document.write('</div>');
                                }
                            </script>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <script type="text/javascript">
                                for(var secCnt=1;secCnt<=secArray.length;secCnt++){
                                    if(secCnt == CurrentSection){
                                        document.write('<div id="viewSection'+secCnt+'" style="display:block">You are viewing all about <b>'+secArray[secCnt-1]+'</b> mobile phone: <br><br>');
                                    }
                                    else{
                                        document.write('<div id="viewSection'+secCnt+'" style="display:none;">You are viewing all about <b>'+secArray[secCnt-1]+'</b> mobile phone: <br><br>');
                                    }
                                }
                            </script>

                        </td>
                    </tr>
                </tbody>
            </table>
        </fieldset>
    </body>
    </html>

And my external javascript file code is:

var CurrentSection =1;

function setSecName(sectioncount,sa){
    CurrentSection=sectioncount;

    load_details(sa);
}

function load_details(sa){
    var secArray =sa;
    for(var secCnt=1;secCnt<=secArray.length;secCnt++){
        if(secCnt == CurrentSection){
            document.getElementById('viewSection'+secCnt).style.display = "block";
        }else{
            document.getElementById('viewSection'+secCnt).style.display = "none";
        }
    }
}

I want to achieve the above functionality using Javascript only.Please help me. Thanks in advance.

ent

dfsq

The problem is that you left detail information div elements unclosed. Corrected code:

for (var secCnt = 1; secCnt <= secArray.length; secCnt++) {
    if (secCnt == CurrentSection) {
        document.write('<div id="viewSection' + secCnt + '" style="display:block">You are viewing all about <b>' + secArray[secCnt - 1] + '</b> mobile phone: <br><br></div>');
    } else {
        document.write('<div id="viewSection' + secCnt + '" style="display:none;">You are viewing all about <b>' + secArray[secCnt - 1] + '</b> mobile phone: <br><br></div>');
    }
}

Note that I added </div> in above code.

Also document.write is not the best way to render and display information. You should probably make use of some static elements or create elements dynamically with document.createElement and appendChild methods.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

javascript addEventlistener "click" not working

From Dev

JavaScript 'click' not working

From Dev

JavaScript .click() not working in IE

From Dev

JavaScript Audio Click Not Working

From Dev

JavaScript : .Click not working (PHP)

From Dev

auto click not working in javascript

From Dev

if statement in JavaScript game not working as desired to allow for collision detection

From Dev

Why trigger click not working in javascript

From Dev

Cloned element on click not working Javascript

From Dev

HTML/Javascript click counter not working

From Dev

Javascript Ctrl + Click not working in Firefox

From Dev

Javascript scroll on anchor click not working

From Dev

addEventListener not working in Javascript but jQuery Click is working

From Dev

query not working with desired output

From Dev

Javascript .click() function completes desired action partially whereas it works perfectly when clicking manually

From Dev

HTML,javascript: on button click validation not working

From Dev

Javascript - Code stops working after click on button

From Dev

Javascript/jQuery Change CSS On Click Not Working

From Dev

Javascript Event Listener on click not working as expected

From Dev

javascript click function not working on form generated in php

From Dev

Javascript delay after click, set timeout not working

From Dev

Why is my javascript button click not working?

From Dev

javascript click event not working in div onclick

From Dev

Single click and Double click on the same element, not working; Javascript

From Dev

Making a Tip Calculator in javascript where users can enter their desired tip% and calculate button isn't working

From Dev

Sendmail not working as desired in bash script

From Dev

_vsnwprintf_s not working as desired

From Dev

Threading lock in python not working as desired

From Dev

Sendmail not working as desired in bash script

Related Related

  1. 1

    javascript addEventlistener "click" not working

  2. 2

    JavaScript 'click' not working

  3. 3

    JavaScript .click() not working in IE

  4. 4

    JavaScript Audio Click Not Working

  5. 5

    JavaScript : .Click not working (PHP)

  6. 6

    auto click not working in javascript

  7. 7

    if statement in JavaScript game not working as desired to allow for collision detection

  8. 8

    Why trigger click not working in javascript

  9. 9

    Cloned element on click not working Javascript

  10. 10

    HTML/Javascript click counter not working

  11. 11

    Javascript Ctrl + Click not working in Firefox

  12. 12

    Javascript scroll on anchor click not working

  13. 13

    addEventListener not working in Javascript but jQuery Click is working

  14. 14

    query not working with desired output

  15. 15

    Javascript .click() function completes desired action partially whereas it works perfectly when clicking manually

  16. 16

    HTML,javascript: on button click validation not working

  17. 17

    Javascript - Code stops working after click on button

  18. 18

    Javascript/jQuery Change CSS On Click Not Working

  19. 19

    Javascript Event Listener on click not working as expected

  20. 20

    javascript click function not working on form generated in php

  21. 21

    Javascript delay after click, set timeout not working

  22. 22

    Why is my javascript button click not working?

  23. 23

    javascript click event not working in div onclick

  24. 24

    Single click and Double click on the same element, not working; Javascript

  25. 25

    Making a Tip Calculator in javascript where users can enter their desired tip% and calculate button isn't working

  26. 26

    Sendmail not working as desired in bash script

  27. 27

    _vsnwprintf_s not working as desired

  28. 28

    Threading lock in python not working as desired

  29. 29

    Sendmail not working as desired in bash script

HotTag

Archive