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>
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.
To enter the text into a textbox using javascript, we have two ways: FindElement(Javascript) + EnterText (Javascript) FindElement(WebDriver) + EnterText (Javascript)
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.
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.
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With