1

with similar question. :)

<input type="text" name="npcolor" id="npcolor" size="9" maxlength="9" value="<?=$userinfo->npcolor?>" onchange="change_npcolor()" readonly />
<input type="text" ID="np_sample" size="2" value="" readonly style="background-color:<?=$userinfo->npcolor?>" />
<input type="button" onclick="pickerPopup202('npcolor','np_sample');" value="Change" />

function pickerPopup202 is changing npcolor, but when npcolor is changed it don't call change_npcolor(). When I put extra button that call change_npcolor it works. I tried also:

document.getElementById("npcolor").onchange='change_npcolor()';

without success.

P.S. JS that changes npcolor (pickerPopup202) isnt mine, and ALL code is at one line, so i cant really mod it.

1
  • 1
    chage event doesn't fire on input, when its value is changed via js. Commented Feb 19, 2011 at 11:45

2 Answers 2

2

When you change the value dynamically, the onchange event doen't fire. You need to call the change_npcolor() method yourself. You could also call document.getElementById("npcolor").onchange(). (This is less efficient, but more flexible when the event handler may change eventually.)

You cannot change the event listener by just adding a string with javascript code to the onchange property. You can do it like this, however:

document.getElementById("npcolor").onchange = function(){
   change_npcolor();
}
Sign up to request clarification or add additional context in comments.

2 Comments

You should avoid two elements with the same id, since getElementById might not work properly in that case. What exactly is te problem?
With a lot of code dividing i modified js. The orig. code was 1 line, 4k symbols. Now it's 120 lines. :) I wanted use onChange if some day i decide to change Color Picker, i just replace it, not modify it again.
0
document.getElementById("npcolor").onchange='change_npcolor()';

here you have change_color() as a string but this is not correct syntax.

Instead of that you can use

document.getElementById("npcolor").onchange=change_ncolor;

Because the change_ncolor works as an object.

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.