开发者

JS/HTML onChange

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 chang开发者_StackOverflow社区ed 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.


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();
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜