optikalefx Posted December 17, 2007 Share Posted December 17, 2007 I have a variable 'i' that is changed by functions. And i want the functions to act based on those changes. I.E a global variable. But i cant get it to work. 'i' doesnt change outside the function <html> <head> <script type="text/javascript"> var i = 3; function reti(i) { alert(i); } function addField(i) { i++; document.getElementById('fields').innerHTML += "<span id='db" + i + "'>Data field " + i + ": <input type='text' name='db" + i + "'><br>\n"; return i; } function delField(i) { document.getElementById('db'+i).innerHTML = ""; return i--; } function liveSearch() { if(document.xmlCreator.livesearch.checked == true) { document.getElementById('liveSearches').innerHTML += "Which field to Display?: <input type'text' size='2' name='ls'><br>\n"; } else { document.getElementById('liveSearches').innerHTML = ""; } } </script> </head> <form name="xmlCreator"> Database name: <input type="text" name="database_name"><br> <div id="fields"> <span id="db1">Data field 1: <input type="text" name="db1"><br> <span id="db2">Data field 2: <input type="text" name="db2"><br> <span id="db3">Data field 3: <input type="text" name="db3"><br> </div> <input type="button" onClick="reti(i);" value="What is i"> <input type="button" onClick="i = addField(i);" value="Add a field"> <input type="button" onClick="i = delField(i);" value="Delete a field"><br> Live Search? <input type="checkbox" name="livesearch" onchange="liveSearch();"><br> <div id="liveSearches"> </div> </form> </html> i can add fields fine, and i can delete 1 field. But i cant delete more than one. and then after you delete one, i is the same as it was, it didnt decrement so the numbers are off. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 17, 2007 Share Posted December 17, 2007 look into addChild(), removeChild(), and appendChild() - that's you best bet for doing something like this. Quote Link to comment Share on other sites More sharing options...
optikalefx Posted December 17, 2007 Author Share Posted December 17, 2007 i know i have to use those when i make the xml file, but before that i want the user to enter values, so i have the text boxes dynamically show up so the user can enter the name of the nodes. Quote Link to comment Share on other sites More sharing options...
markjoe Posted December 20, 2007 Share Posted December 20, 2007 (non-expert alert) What about making it a (pseudo)class? I made my first one only a few days ago. I have had global variable problems like this in the past. I have not, however, had any issue refering to "this.variable" in a method. Or, sometimes I fall back on a hidden input in palce of a global variable, ugly way of doing things, but it works. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.