Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass text in a textbox to JavaScript function?

Suppose I have the following HTML code, how can I pass the user's input to execute(str) JavaScript function as an argument?

<body>  <input name="textbox1" type="text" /> <input name="buttonExecute" onclick="execute(//send the user's input in textbox1 to this function//)" type="button" value="Execute" />  </body> 
like image 372
natch3z Avatar asked Apr 19 '09 10:04

natch3z


People also ask

Which JavaScript function is used to enter the value inside the text field?

We can get the value of the text input field using various methods in JavaScript. There is a text value property that can set and return the value of the value attribute of a text field. Also, we can use the jquery val() method inside the script to get or set the value of the text input field.

How do you enter a text box in JavaScript?

To enter the text into a textbox using javascript, we have two ways: FindElement(Javascript) + EnterText (Javascript) FindElement(WebDriver) + EnterText (Javascript)

How do you pass text in HTML?

Step 2: Now, we have to place the cursor before that text which we want to move. And, then we have to define the <marquee> tag, which is used for moving the text on the web page. So, type the open <marquee> tag before the text we want to move and close the <marquee> tag just after that text.

How pass parameter from JavaScript to HTML?

Use the onclick attribute in a button tag with the function name and pass value in this function. With this method, you can also take input from users and pass parameters in the JavaScript function from HTML.


1 Answers

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" /> 
like image 50
Gumbo Avatar answered Oct 06 '22 01:10

Gumbo