Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / February 2005



Tip: Looking for answers? Try searching our database.

What wrong with the code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Smith - 28 Feb 2005 15:04 GMT
<!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="navcontainer">
<ul id="navlist">
<li id="active"><a href="#" id="current">Item 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.getElementById('navlist');
if (nav){
 var links = nav.getElementsByTagName('a');
 for (var i=0;i<links.length;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 - 28 Feb 2005 15:16 GMT
> <div id="navcontainer">
> <ul id="navlist">
[quoted text clipped - 15 lines]
>    //alert(str);
>    links[i].onclick = function(){ alert(str); };

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

Signature

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

Michael Winter - 28 Feb 2005 15:37 GMT
[snip]

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

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.

> function initNavlists(){
>  var nav = document.getElementById('navlist');
[quoted text clipped - 8 lines]
>  }
> }

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

[snip]

> Why i always get the last item in the alert box?

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.innerHTML);
    }

    /* ... */

    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.com/faq/faq_notes/not_browser_detect.html>

Signature

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

Grant Wagner - 28 Feb 2005 21:44 GMT
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
[quoted text clipped - 30 lines]
> Why i always get the last item in the alert box?
> How to get the rest?

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

Or:

<script defer="false" type="text/javascript">
function initNavlists(){
var nav = document.getElementById('navlist');
function clicked(s) { return function() { alert(s); } }
if (nav){
 var links = nav.getElementsByTagName('a');
 for (var i=0;i<links.length;i++)
 {
  var str = links[i].innerHTML;
  //alert(str);
  links[i].onclick = clicked(s);
 }
}
}
initNavlists();
</script>

Signature

Grant Wagner <gwagner@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq 

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.