How to get all html elements with a fixed start string

advertisements

I have many html elements with id like

        "demo1"
        "demo2"
        "demo3"

and many more

        "demoNth"

how to get all the elemnts using jquery?


You can use the following :

this will get all elements starting with demo

$("[id^=demo]")

Selector documentation here

Iterate over them using each() ->

$("[id^=demo]").each(function(index) {
    alert(index + ': ' + $(this).text());
});

each documentation here