The problem is that $$() returns a list of elements matching the CSS selector
try this if you just want the first element to match 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 elements that match the CSS selector 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 because writeAttribute() is a PrototypeJS method and will work in all browsers supported by PrototypeJS, setAttbribute() is a standard method that is not always available.
source share