Search code examples
jqueryprototypejs

Change text inside a button in prototype


I have a button with class 'scalable' inside a div with id 'product_form', i want to change the text of the span within that button, i have the following jQuery script to get what i want.

<form id='product_form'>
    <button class='scalable' name='submit'>
        <span>old Text</span>
    </button>
</form>

I want to change the text 'old text' in the button to 'new text' inside the span within the button using the form id and i want to do the same using prototype.

Here is the jQuery equivalent code:

jQuery('#product_form button.scalable span').text('new text');

But i don't have jQuery and i wanna perform the same task in prototype.

please suggest me the equivalent code for the above in prototype.

Thanks.


Solution

  • For CSS selectors use $$ which returns an Array. One way to proceed is to assume only the first element.

    $$('#product_form button.scalable span').first().update('new text');
    

    Alternatively, you can affect all items in the array.

    $$('#product_form button.scalable span').invoke('update', 'new text');