I just wanna know how I get the value of a text field in the onClick of a button. I got this:
<INPUT TYPE="text" NAME="GetLink_textField" VALUE="Geben Sie den Dateipfad an!" SIZE=50>
<INPUT TYPE="button" NAME="GetLink_button" VALUE="Get link" onClick="getLinkToFile(?????)">
I want to pass the function the value of the text field GetLink_textfield. How do I do this? I tried GetLink_textfield.value but that doesnt work.
I just wanna know how I get the value of a text field in the onClick of a button. I got this:
<INPUT TYPE="text" NAME="GetLink_textField" VALUE="Geben Sie den Dateipfad an!" SIZE=50>
<INPUT TYPE="button" NAME="GetLink_button" VALUE="Get link" onClick="getLinkToFile(?????)">
I want to pass the function the value of the text field GetLink_textfield. How do I do this? I tried GetLink_textfield.value but that doesnt work.
Share Improve this question asked Jan 31, 2012 at 7:44 Tobi WeißhaarTobi Weißhaar 1,6776 gold badges26 silver badges35 bronze badges 2- try using the 'this' variable to reference the object which just got clicked – hackartist Commented Jan 31, 2012 at 7:48
- hackartist: The OP is not trying to access the object that was clicked. The only way OP would be able to get the object they want with this, would be as follows: this.previousSibling.value; Not ideal. – Shane Commented Jan 31, 2012 at 7:53
4 Answers
Reset to default 3The reason GetLink_textfield.value did not work is because there is no object called GetLink_textfield.
You will need to use the following code:
function getLinkToFile() {
var e = document.getElementsByName('GetLink_textField')[0];
var val = e.value;
// Do the rest here
}
document.getElementsByName("GetLink_textField")[0].value
It would probably be better if you give the textfield an ID, however, and use document.getElementById("GetLink_textField").
That way, you'll get the object, and not an array containing it, since ID's have to be unique, but names don't.
add an ID attribute to the text field and then use that ID in the javascript function to get the value. You do not need to send any arguement.Example-
function getLinkToFile(){
var textVal=document.getElementById('idname').value;
}
If you are generating the inputs programmatically or want to use more than 1 set (input button, input text) of elements, try to do some tricky thing like this:
Do a "for" or something and append the "for" variable (usually known as "i") to the input text name, button and javascript function and the result will generate something like this:
<INPUT TYPE="text" NAME="GetLink_textField_0" VALUE="Geben Sie den Dateipfad an!" SIZE=50>
<INPUT TYPE="button" NAME="GetLink_button_0" VALUE="Get link" onClick="getLinkToFile(0)">
<INPUT TYPE="text" NAME="GetLink_textField_1" VALUE="Geben Sie den Dateipfad an!" SIZE=50>
<INPUT TYPE="button" NAME="GetLink_button_1" VALUE="Get link" onClick="getLinkToFile(1)">
<INPUT TYPE="text" NAME="GetLink_textField_2" VALUE="Geben Sie den Dateipfad an!" SIZE=50>
<INPUT TYPE="button" NAME="GetLink_button_2" VALUE="Get link" onClick="getLinkToFile(2)">
Then in your function:
function getLinkToFile(position){
var inputVal = document.getElementsByName("GetLink_textField_" + position)[position].value;
//now you got inputVal of the element in the position you want.
}
Hope this helps and if this is not what you need you can try examples from above.
Also, is a good remendation to use Id's for your inputs.
Regards!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744239162a4564618.html
评论列表(0条)