-2

I try to pass two id's from my list to function, But my function unable to recognize the id's.

My code as follow:

<html>
<head>
<script language="javascript"> 
function toggle(id1, id2)   {    
var ele = document.getElementById(id2);  
var text = document.getElementById(id1);  
    if(ele.style.display == "block") {  
            ele.style.display = "none";
        text.innerHTML = "NDEM10K";
    }
    else {
        ele.style.display = "block";
        text.innerHTML = "NDEM10K";
    }
} 
</script>
</head>
 <body>
 <ol>
    <li>
        <a id="displayText" href="javascript:toggle(this.id);">NDEM10K</a> 
        <div id="toggleText" style="display: none" onclick="toggle(this.id);">
        <ul>
            <li>AP</li>
            <li>UP</li>
            <li>KA</li>
            <li>An</li>
            <li>ts</li>
        </ul>
            </div>
    </li>
</ol>
</body>
</html>

My function unable to recognise id's. So, I want to assign id to a variable.

My Full code is here:

<html>
<head>


<script language="javascript"> 
function toggle(id1,id2) {
    var ele = document.getElementById(id1);
    var text = document.getElementById(id2);
    if(ele.style.display == "block") {
            ele.style.display = "none";
        text.innerHTML = "NDEM10K";
    }
    else {
        ele.style.display = "block";
        text.innerHTML = "NDEM10K";
    }
} 


</script>
</head>
 <body>
 <ol>
    <li>
        <a id="displayText" href="javascript:toggle(this.id);">NDEM10K</a> 
        <div id="toggleText" style="display: none" onclick="toggle(this.id);">

        <ul>
            States
            <li>Andhra</li>
            <li>Andhra</li>
            <li>Andhra</li>
            <li>Andhra</li>
            <li>Andhra</li>
        </ul>

            </div>
    </li>
    <li>
        <a id="dT" href="javascript:toggle(this.id);">NDEM50K</a> 
        <div id="tT" style="display: none" onclick="NDEM50K(this.id);">

        <ul>
            Districts
            <li>East Godavari</li>
            <li>West Godavari</li>
            <li>Krishna</li>
            <li>Vishaka</li>
            <li>Hyderabad</li>
        </ul>

            </div>
    </li>
    <li>
        <a id="2dT" href="javascript:toggle(this.id);">NDEM2K</a> 
        <div id="2tT" style="display: none" onclick="NDEM2K(this.id);">

        <ul>
            Districts
            <li>Hyderabad</li>
            <li>Kakinada</li>
            <li>Rajahmundry</li>
            <li>Vishaka</li>
            <li>Mumbai</li>
        </ul>

        </div>
    </li>

</ol>
</body>
</html>
Satya Chandra
  • 728
  • 2
  • 12
  • 26

3 Answers3

1

You are passing one id at a time, thats why your are getting error property of undefined. And why you want to pass id and create element in function, if you can pass elements itself in the function.

Try This:

<html>
 <head>
    <script language="javascript">
        function toggle(ele, text) {
            if (ele !== null && ele !== undefined && ele.style.display == "block") {
                ele.style.display = "none";
                text.innerHTML = "NDEM10K";
            }
            else {
                ele.style.display = "block";
                text.innerHTML = "NDEM10K";
            }
        }
    </script>
</head>
<body>
    <ol>
        <li>
            <a id="displayText" onclick="toggle(this.nextSibling.nextSibling, this)"     href="#">NDEM10K</a> 
            <div id="toggleText" style="display: none" onclick="toggle(this, this.previousSibling.previousSibling);">
                <ul>
                    <li>AP</li>
                    <li>UP</li>
                    <li>KA</li>
                    <li>An</li>
                    <li>ts</li>
                </ul>
            </div>
        </li>
    </ol>
 </body>
</html>
Rahul Munjal
  • 2,551
  • 2
  • 15
  • 35
0

Based on everything you said in the comments I think you want this:

This

    <div id="toggleText" style="display: none" onclick="toggle(this.id);">

Should Be

    <div id="toggleText" style="display: none" onclick="toggle(this.id,this.parentNode.firstChild.id);">
Joe Thomas
  • 5,807
  • 6
  • 25
  • 36
0

Since your ids will be constant strings, you can try by passing directly ids. Like,

toggle('displayText','toggleText')

Bharadwaj
  • 2,535
  • 1
  • 22
  • 35