1

i have a input and i want access value of input and change it with inline callback function . because of some reason i can not select input with id or class or anything else and i have to use the callback function my input :

<input type = 'text' name = 'input_name' onkeyup = 'my_func()' /> 

with this i can access the value and send it to my_func but i can not change it

<input type = 'text' name = 'input_name' onkeyup = 'my_func(this.value)' />


function my_func(value){
 alert(value);
 }

in above function when user type something i want change the value of input. how can i change that?

0

4 Answers 4

1

You can either write your javascript directly into the onkeyup event like so:

<input type = 'text' name = 'input_name' onkeyup = 'this.value="custom text"' />

Or, you can pass this through into your function, and then modify the .value property within your function:

function my_func(elem) {
  elem.value = "custom text";
}
<input type='text' name='input_name' onkeyup='my_func(this)' />

Note: At the moment your event is only firing when you let go of the key. Consider using oninput event instead of onkeyup event:

function my_func(elem) {
  elem.value = "custom text";
}
<input type='text' name='input_name' oninput='my_func(this)' />

Sign up to request clarification or add additional context in comments.

Comments

0

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input

value = The input's current value

<input type = 'text' name = 'input_name' onkeyup = 'my_func(this)' />


function my_func(input){
   input.value = "hello!"
}

Comments

0

You can pass the element in the function and change the value in the function

function my_func(e){
e.value="hey!"
 alert(e.value);
 }
<input type = 'text' name = 'input_name' onkeyup = 'my_func(this)' />

Comments

0

You can give your input an id attribute and access it using document.getElementById method. Using this you then change value of the input.

Something like

document.getElementById(‘inputID’).value = ‘hello’;

Comments

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.