Hide divs with similar ID

7

I have several div tags with similar id, they all start with cn - , example:

<div id="cn-name">...</div>
<div id="cn-email">...</div>
<div id="cn-pass">...</div>

How can I interact with all of them without having to specify one by one? Was there any way she could do this at the beginning? For example:

/* no css */
#cn-* {
  display:none;
}

/* no javascript/jquery */
$('#cn-*').hide();
    
asked by anonymous 08.08.2016 / 15:50

1 answer

9

See these two options

CSS:

div[id^='cn-'] {
  display:none;
}

or jQuery:

$('div[id^="cn-"]').hide();

This will allow you to select all elements that contain cn- at the beginning of the ID.

Retired from SOen: css-selector-id-contains-part-of -text

Selectors:

  • [atributo^=valor] : selects all elements that atributo starts with valor
  • [atributo$=valor] : selects all elements that atributo ends with valor
  • [atributo*=valor] : selects all elements that atributo contains valor

Source: CSS Selectors

    
08.08.2016 / 15:59