Search code examples
javascriptprototypejsselector

Prototype.js: select elements with id starts with?


There are tags with attributes id like this

<span id="attr35"></span>
<span id="attr44"></span>
<span id="attr23"></span>

Need to set the style to them like this (last two digits might be any)

$("span[id=attr???]").setStyle({'display':'inline'});

Is it possible?


Solution

  • You use valid Selectors API selectors.

    $("span[id^=attr]")
    

    If you want more than one match, use $$

    $$("span[id^=attr]")
    

    Oops, one more issue. You should use .invoke if you are getting multiple matches. You can't call setStyle directly on the returned set.

    $$("span[id^=attr]").invoke("setStyle", ...)