Jump to content

global variables or points or something i can modify


optikalefx

Recommended Posts

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.

 

(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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.