top of page

Ask a Question

Public·6 members

How to pass text in a textbox to JavaScript function?

You could either access the element’s value by its name:


document.getElementsByName("textbox1"); // returns a list of elements with name="textbox1"

document.getElementsByName("textbox1")[0] // returns the first element in DOM with name="textbox1"

so

<input name="buttonExecute" onclick="execute(document.getElementsByName('textbox1')[0].value)" type="button" value="Execute" />

Or you assign an ID to the element that then identifies it and you can access it with getElementById:


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

<input name="buttonExecute" onclick="execute(document.getElementById('textbox1').value)" type="button" value="Execute" />

or this will also work


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

<input name="buttonExecute" onclick="execute(textbox1.value)" type="button" value="Execute" />

Please see the example of the code:


https://www.codersarts.com/computer-science-assignment-forum/ask-a-question/convert-celsius-to-fahrenheit-in-javascript

269 Views
bottom of page