admin管理员组文章数量:1287971
I want to change the button onclick function inside a div. The HTML code is as bellow
<div class="buttons-set" id="payment-buttons-container">
<button type="button" class="button" onclick="payment.save()" id="payment_button"></button>
</div>
I am using the following prototype to change the onlick function payment.save() to something else.
if(payment_method=='gate') {
var e = $$('div#payment-buttons-container button.button');
e.setAttribute('onclick','return false;'); // not working
} else {
var e = $$('div#payment-buttons-container button.button');
e.setAttribute('onclick','payment.save();'); // not working
}
Which is not working and saying "Uncaught TypeError: Object [object HTMLButtonElement] has no method 'setAttribute'"But when i am using this code by taking the id of the button statically, than its working
if(payment_method=='gate') {
$('payment_button').setAttribute('onclick','return false;');// Works
}
else {
$('payment_button').setAttribute('onclick','payment.save();');// Works
}
Where "payment_button" is the ID of the button which i taken.
I want to change the button onclick function inside a div. The HTML code is as bellow
<div class="buttons-set" id="payment-buttons-container">
<button type="button" class="button" onclick="payment.save()" id="payment_button"></button>
</div>
I am using the following prototype to change the onlick function payment.save() to something else.
if(payment_method=='gate') {
var e = $$('div#payment-buttons-container button.button');
e.setAttribute('onclick','return false;'); // not working
} else {
var e = $$('div#payment-buttons-container button.button');
e.setAttribute('onclick','payment.save();'); // not working
}
Which is not working and saying "Uncaught TypeError: Object [object HTMLButtonElement] has no method 'setAttribute'"But when i am using this code by taking the id of the button statically, than its working
if(payment_method=='gate') {
$('payment_button').setAttribute('onclick','return false;');// Works
}
else {
$('payment_button').setAttribute('onclick','payment.save();');// Works
}
Where "payment_button" is the ID of the button which i taken.
Share Improve this question edited Feb 1, 2013 at 11:31 tinkesh asked Feb 1, 2013 at 10:27 tinkeshtinkesh 3133 gold badges11 silver badges25 bronze badges1 Answer
Reset to default 10The problem is that $$()
returns a list of elements that match the CSS selector
try this if you just want the first element the matches the selector
if(payment_method=='gate') {
var e = $$('div#payment-buttons-container button.button')[0];
e.setAttribute('onclick','return false;'); // not working
} else {
var e = $$('div#payment-buttons-container button.button')[0];
e.setAttribute('onclick','payment.save();'); // not working
}
if you want to apply it to all the elements that the CSS selector matches do it this way using the invoke()
method
if(payment_method=='gate') {
$$('div#payment-buttons-container button.button').invoke('writeAttribute','onclick','return false;');
} else {
$$('div#payment-buttons-container button.button').invoke('writeAttribute','onclick','payment.save();'); // not working
}
the reason I used writeAttribute()
instead of setAttribute()
is that writeAttribute()
is the PrototypeJS method and will work across all the browsers that PrototypeJS supports, setAttbribute()
is the standard method that is not always available.
本文标签: javascriptprototype setAttribute() not working with () selectorStack Overflow
版权声明:本文标题:javascript - prototype setAttribute() not working with $$() selector - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741329665a2372694.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论