views:

77

answers:

3

i'm trying to get the value of 2 textboxes with Javascript.

textbox 1, nothing wrong.

textbox 2, same code, there happends nothing!!!!!!

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???

+5  A: 

I recommend modifying your debugging practices to make it easier on yourself than using alerts:

  • Run a browser with a js console (e.g. Chrome or Firefox).
  • Run js in the console to check for values where something goes wrong in the static code, e.g.
    • run 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.
  • You can also run any arbitrary javascript of your choice in the console, with clickable access to the available javascript objects created by the page loading. This somewhat lessens the need for manually coding in alerts.
Tchalvak
Seriously, the days when `alert` was your only option are way behind us!
ChaosPandion
i'll send the html
Alpjef
A: 
<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

Alpjef
A: 
javascript:var val = 1;alert(document.getElementById("textfield"+val).value)

When i use this code in the javaconsole of firefox, nothing wrong!

Alpjef