Get HTML element using jQuery

I have some DIVs with identifier "div1", "div2" ... "divx". I want to perform an operation on them. Is there a way to get the id of the .contains "div" element. Any other idea?

+5
source share
3 answers

You can use starts with a selector

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

The above will capture all divs that have an attribute idthat starts with letters div.

An example jsFiddle that makes all divs with id div...invisible.


, , div , div, - . , div1 , divx .

filter() . true, false, . match(), ( [0-9] [\d]):

$("div").filter(function() {
    return $(this).attr("id").match(/^div[\d]+$/)
})

attr() , id.

jsFiddle, div , div, .

+11

( ):

jQuery("div[id^='div']")

div, id, "div".

div, id , "div", :

jQuery("div[id*='div']")
+7

, div1, div2, div3 . "black-bold-12px", . .

- .

, :

<div class="something usethis">...</div>
<div class="usethis something_else">...</div>
<div class="usethis">...</div>
<div class="something anotherclass usethis" id="someId">...</div>

<script>
  $(".usethis").html("New contents for all of them!");
</script>
+2

All Articles