14

I have a JavaScript input object with type="text" coming along with an onchange event which is created by (related lines):

var id_box = document.createElement('input');
id_box.id = 'id_box';
id_box.type = 'text';
id_box.onchange = function()
{
    alert("Changed!");
}

For changing this value in this input field I use:

var a = document.createElement('a');
a.innerHTML = "A";
a.onclick = function()
{
    document.getElementById('id_box').value = 'A';
}

The onchange event works only changing value by keyboard typing but not for changing value by above function. Is there any way to make this works?

Please take a look at this: http://jsfiddle.net/zCMdV/7/

0

1 Answer 1

8

What browser are you using?

I have made this example, which works for me... the subtle change is that I add the event to the input before I append it to the div element.

<div id="test"></div>
<script type="text/javascript">
    var id_box = document.createElement('input');
    id_box.type = 'text';
    id_box.onchange = function()
    {
        alert("Changed!");
    }
    document.getElementById("test").appendChild(id_box);

</script>

See it in action on JS Fiddle: http://jsfiddle.net/Sohnee/zCMdV/

Update:

If you are going to automate the changing of the value, you can do the same to trigger the change event...

http://jsfiddle.net/Sohnee/zCMdV/10/

document.getElementById('id_box').onchange();
Sign up to request clarification or add additional context in comments.

2 Comments

I just found that it's not the code problem but the way to change the value in input field. Please read my question again.
I'd suggest you remove the non-answer part of your answer, as it threw me off, and could very well be throwing others off..

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.