26

Lets say I have a form as below. How do I pass the value in the textbox named "configname" to the onclick function handler??

<form id="loadconfigform">
        Config Name: <input type="text" name="configname" />
        <input type="button" value="Submit" onclick="onLoadConfigPress(configname)" />
    </form>

4 Answers 4

40

Give an id to input field:

<input type="text" id="configname" name="configname" />

Now modify click handler as follows:

<input type="button" value="Submit" 
  onclick="onLoadConfigPress(document.getElementById('configname').value)" />

Or if you have only one form on that page, you could also use forms array:

<input type="button" value="Submit" 
  onclick="onLoadConfigPress(document.forms[0].configname.value)" />
Sign up to request clarification or add additional context in comments.

Comments

3
<form id="loadconfigform">
   Config Name: <input type="text" id="configname" name="configname" />
   <input type="button" value="Submit"
     onclick="onLoadConfigPress(document.getElementById('configname').value);" />
</form>

1 Comment

@user...... id must be a unique identifier of element. document.getElementById(...) quickly find and return pointer to the element if found.
2

This is an old, old question - but somehow I ended up here.

So, the magic word 'this' is your friend here. 'this' is the element that is clicked on, which will include the value of the element. So

<form id="loadconfigform">
    Config Name: <input type="text" name="configname" />
    <input type="button" value="Submit" onclick="onLoadConfigPress(this)" />
</form>

function onLoadConfigPress(el){

    var configName = el.value;
    // do other things
    // . . .
}

Side note: if you want to pass the original event along with 'this', include it as the first parameter as the magic word 'event'.

onclick="onLoadConfigPress(event,this)

Comments

1
<form id="loadconfigform">
        Config Name: <input type="text" name="configname" />
        <input type="button" value="Submit" onclick="onLoadConfigPress(document.getElementsByName('configname')[0].value)" />
    </form>

Simply call it using its name. I'd recommend using ID though.

This won't work if you have other elements with the same name, so do use ID like the other answers have suggested.

3 Comments

This cannot works. name is not unique and you must switch to use document.getElementById('loadconfigform').getElementsByName(...)[0] because other elements can have same name attribute and can be placed before our element
Similarly, other elements CAN be given different names as well, but in practice, I haven't seen that happen. I know he should be using ID, though. I was just giving him a solution to his specific problem here.
Your solution to his specific problem does not always works. You must write about this in answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.