469,315 Members | 1,510 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

variable increment problem

10
This is my code:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Untitled Document</title>
  7. <style type="text/css" id="mainStyle">
  8. #floatingDiv {
  9.     position:absolute;
  10.     border:#000000 solid 1px;
  11.     font-family:Arial, Helvetica, sans-serif;
  12.     font-size:14px;
  13.     margin-top:0;
  14.     padding-top:0;
  15. }
  16. #floatingDiv ul li:hover {
  17.     background-color:#333333;
  18.     color:#CCCCCC;
  19.     cursor:pointer;
  20. }
  21. #floatingDiv ul {
  22.     padding:0;
  23.     margin:0;
  24.     list-style:none;
  25. }
  26. </style>
  27. <script type="text/javascript">
  28. function changeText(value) {
  29.     document.mainForm.textBox.value = value;
  30. }
  31. var li_n = 0;
  32. function populateList(){
  33.     objList = document.getElementById('floatingDivUL');
  34.  
  35.     objListElement = document.createElement('li');
  36.     objTextNode = document.createTextNode('Test Element'+li_n+'');
  37.     if(navigator.appName != "Microsoft Internet Explorer") {
  38.         objListElement.setAttribute("onclick","changeText('Test Element"+li_n+"')");
  39.     } else {
  40.         objListElement.setAttribute("onclick",function(){changeText('Test Element'+li_n)});
  41.     }
  42.     objListElement.setAttribute("id","floatingDivLi"+li_n);
  43.  
  44.     objListElement.appendChild(objTextNode);
  45.     objList.appendChild(objListElement);
  46.  
  47.     CheckEvents();
  48.  
  49.     li_n++;
  50. }
  51. function CheckEmpty() {
  52.     if(document.getElementById('floatingDivUL').innerHTML == "") {
  53.         document.getElementById('floatingDiv').style.border = "";
  54.     }else{
  55.         document.getElementById('floatingDiv').style.border = "#000000 solid 1px";
  56.     }
  57. }
  58. function CheckEvents() {
  59.     CheckEmpty();
  60. }
  61. window.onload = setTimeout("CheckEvents()",10);
  62. </script>
  63. </head>
  64.  
  65. <body>
  66. <form name="mainForm">
  67.     <input name="textBox" type="text" /><a href="javascript:populateList()">clcik</a><br />
  68.     <div id="floatingDiv">
  69.         <ul id="floatingDivUL">
  70.  
  71.         </ul>
  72.     </div>
  73. </form>
  74. </body>
  75. </html>
  76.  
My problem is that the changeText() function where it looks like this function(){changeText('Test Element'+li_n)} does not use the li_n variable where it was initiated but where it is at after incrementation.
Oct 7 '07 #1
1 1398
gits
5,390 Expert Mod 4TB
hi ...

try not to use setAttribute for the onclick, use something like in the following example:

Expand|Select|Wrap|Line Numbers
  1. // this variable 'li_n' must be global and increment it 
  2. // the way you want
  3. var li_n = 0; 
  4.  
  5. function changeText(val) {
  6.     alert(val);
  7. }
  8.  
  9. var click_handler = function(e) {
  10.      var val = 'Test Element' + li_n;
  11.  
  12.      return changeText(val);
  13. };
  14.  
  15. your_obj.onclick = click_handler;
  16.  
kind regards
Oct 7 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by G. | last post: by
166 posts views Thread by Graham | last post: by
1 post views Thread by Nathan Sokalski | last post: by
5 posts views Thread by Jeff | last post: by
37 posts views Thread by minkoo.seo | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.