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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.