Reputation: 31
I'm trying to get the value of 2 textboxes with Javascript.
Textbox 1, nothing wrong.
Textbox 2, same code, but nothing happens.
Here is the code
var fieldname;
fieldname = document.getElementById("div"+field).getAttribute("field");
alert(fieldname); // RETURNS "Birthdate"
var textval;
textval = document.getElementById("textfield"+field).value;
alert(textval); // RETURNS NOTHING
var field
is the id of the textbox and div
.
Why isn't this working?
Upvotes: 1
Views: 256
Reputation: 23062
When posting html/javascript/css problems like this, isolate the code and put it in a jsfiddle ( jsfiddle.net ). Not only will this allow others to much more quickly solve your problem for you, but often isolating the code from your environment cuts out extra factors that may have been causing unexpected behavior. Often the process of moving a snippet to jsfiddle.net can show the problem before anyone else even needs to get involved!
Secondly, I recommend modifying your debugging practices to make it easier on yourself than using alerts:
To ensure graceful degredation of console.log, add this code to your global javascript:
if (typeof(console) == 'undefined') { console = { log: function() { } }; }
Run js in the console to check for values where something goes wrong in the static code, e.g.
document.getElementById("textfield"+field).value
to check whether the value you expect is available, and then walk back down the line if you don't find what you're expecting, e.g. if .value
isn't available, run document.getElementById("textfield"+field)
and expand the object to view it's contents, etc.Upvotes: 6
Reputation: 31
javascript:var val = 1;alert(document.getElementById("textfield"+val).value)
When i use this code in the javaconsole of firefox, nothing wrong!
Upvotes: 0
Reputation: 31
<div id="div<?php echo $aantal;?>" field="<?php echo $rows['field'];?>">
<h3><?php echo $rows['name'];?>:</h3>
<p id="p<?php echo $aantal;?>">
<input type="text" id="textfield<?php echo $aantal;?>" value="<?php echo $rows['default'];?>" MAXLENGTH="<?php echo $rows['length'];?>"/><br/>
<input type="button" id="buttonacc<?php echo $aantal;?>" value="Verzend" onClick="acc('<?php echo $aantal;?>');"/>
<input type="button" id="buttonden<?php echo $aantal;?>" value="Nu niet!" onClick="den('<?php echo $aantal;?>');"/>
</p>
</div>
This is the HTML
Upvotes: 0