What wrong with the code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Smith

    #1

    What wrong with the code

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    <div id="navcontaine r">
    <ul id="navlist">
    <li id="active"><a href="#" id="current">It em one</a></li>
    <li><a href="#">Item two</a></li>
    <li><a href="#">Item three</a></li>
    <li><a href="#">Item four</a></li>
    <li><a href="#">Item five</a></li>
    </ul>
    </div>
    <script defer="false" type="text/javascript">
    function initNavlists(){
    var nav = document.getEle mentById('navli st');
    if (nav){
    var links = nav.getElements ByTagName('a');
    for (var i=0;i<links.len gth;i++)
    {
    var str = links[i].innerHTML;
    //alert(str);
    links[i].onclick = function(){ alert(str); };
    }
    }
    }

    initNavlists();
    </script>
    </body>
    </html>

    Why i always get the last item in the alert box?
    How to get the rest?


  • Martin Honnen

    #2
    Re: What wrong with the code



    John Smith wrote:
    [color=blue]
    > <div id="navcontaine r">
    > <ul id="navlist">
    > <li id="active"><a href="#" id="current">It em one</a></li>
    > <li><a href="#">Item two</a></li>
    > <li><a href="#">Item three</a></li>
    > <li><a href="#">Item four</a></li>
    > <li><a href="#">Item five</a></li>
    > </ul>
    > </div>
    > <script defer="false" type="text/javascript">
    > function initNavlists(){
    > var nav = document.getEle mentById('navli st');
    > if (nav){
    > var links = nav.getElements ByTagName('a');
    > for (var i=0;i<links.len gth;i++)
    > {
    > var str = links[i].innerHTML;
    > //alert(str);
    > links[i].onclick = function(){ alert(str); };[/color]

    How about
    links[i].onclick = function (evt) { alert(this.inne rHTML); };


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Michael Winter

      #3
      Re: What wrong with the code

      John Smith wrote:

      [snip]
      [color=blue]
      > <script defer="false" type="text/javascript">[/color]

      Last time I looked, defer was a boolean attribute. A boolean attribute
      does not have "true" and "false" values: its presence means "true" and
      its absence "false". You seem to want the latter so don't include the
      attribute at all.

      The defer attribute can only have one value - defer. That is,

      <script type="text/javascript" defer="defer">

      In HTML, the value can be omitted entirely.
      [color=blue]
      > function initNavlists(){
      > var nav = document.getEle mentById('navli st');
      > if (nav){
      > var links = nav.getElements ByTagName('a');
      > for (var i=0;i<links.len gth;i++)
      > {
      > var str = links[i].innerHTML;
      > //alert(str);
      > links[i].onclick = function(){ alert(str); };
      > }
      > }
      > }[/color]

      Don't forget to feature-detect[1] those DOM methods before using them.

      [snip]
      [color=blue]
      > Why i always get the last item in the alert box?[/color]

      The function expression you assign to the onclick property forms a
      closure preventing the str variable, among other things, from being
      garbage collected. Even though the function objects will be unique,
      they will all have the same scope chain sharing the same variables. In
      other words, str will be the same for all of them, and by the end of
      the loop, str will contain the innerHTML value of the last link.

      Though you could take a more complex route to solve this, it would
      just be a waste of memory and time. The simplest thing to do is to
      refer to the innerHTML property directly:

      function initNavLists() {
      function listener() {
      alert(this.inne rHTML);
      }

      /* ... */

      for(var i = 0, n = links.length; i < n; ++i) {
      links[i].onclick = listener;
      }
      }

      Notice that the this operator will refer to element on which the
      listener function was fired.

      Hope that helps,
      Mike


      [1] <URL:http://www.jibbering.c om/faq/faq_notes/not_browser_det ect.html>

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Grant Wagner

        #4
        Re: What wrong with the code

        "John Smith" <someone@nospam .com> wrote in message
        news:42233303$1 _2@news.tm.net. my...[color=blue]
        > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        > <html xmlns="http://www.w3.org/1999/xhtml">
        > <body>
        > <div id="navcontaine r">
        > <ul id="navlist">
        > <li id="active"><a href="#" id="current">It em one</a></li>
        > <li><a href="#">Item two</a></li>
        > <li><a href="#">Item three</a></li>
        > <li><a href="#">Item four</a></li>
        > <li><a href="#">Item five</a></li>
        > </ul>
        > </div>
        > <script defer="false" type="text/javascript">
        > function initNavlists(){
        > var nav = document.getEle mentById('navli st');
        > if (nav){
        > var links = nav.getElements ByTagName('a');
        > for (var i=0;i<links.len gth;i++)
        > {
        > var str = links[i].innerHTML;
        > //alert(str);
        > links[i].onclick = function(){ alert(str); };
        > }
        > }
        > }
        >
        > initNavlists();
        > </script>
        > </body>
        > </html>
        >
        > Why i always get the last item in the alert box?
        > How to get the rest?[/color]

        Change links[i].onclick = function(){ alert(str); };
        To links[i].onclick = function() { alert(this.inne rHTML); };

        Or:

        <script defer="false" type="text/javascript">
        function initNavlists(){
        var nav = document.getEle mentById('navli st');
        function clicked(s) { return function() { alert(s); } }
        if (nav){
        var links = nav.getElements ByTagName('a');
        for (var i=0;i<links.len gth;i++)
        {
        var str = links[i].innerHTML;
        //alert(str);
        links[i].onclick = clicked(s);
        }
        }
        }
        initNavlists();
        </script>

        --
        Grant Wagner <gwagner@agrico reunited.com>
        comp.lang.javas cript FAQ - http://jibbering.com/faq


        Comment

        Working...