Search code examples
javascriptjquerydomprototypejs

Select div using wildcard ID


How to select a div using it's ID but with a widcard?

If the DIV's ID is statusMessage_1098, I would like to select it in some way like document.getElementById('statusMessage_*').

This is because until the page is generated, I don't know the suffix of the ID and only one such ID will be present in a page. Is this possible?

Thanks for any help.


Solution

  • Using jQuery you can do this

    $("div[id^='statusMessage_']")
    

    See attributeStartsWith

    Edit - with class name selector

    If you can use a class name then you can use something like this

    $$('div.myDivClass');
    

    gets all div elements with class 'myDivClass'