alsanal
alsanal

Reputation: 183

use a blank class to identify elements or other tag

Can I use a "blank" class to identify common elements of the dom in order to perform operations with jquery? I know the tag Id and Name must be unique in the dom, but, if i need identified similar elements but not necessary with the same styling... For example, in this scenario:

<section class="custom-section identify1"></section>
<section class="custom-section identify1"></section>
<section class="custom-section identify2"></section>
<section class="custom-section identify1"></section>
<section class="custom-section identify2"></section>
<section class="custom-section identify1"></section>

In this scenario custom-section styling the section and identifyX group similar sections... ¿is correct? And now with jquery i can get all section with identifyX:

$(".identify1)

I know in html5 exist custom data atributte (Data-*) custom data HTML5 Can I use this tag for this functionallity?

Upvotes: 2

Views: 64

Answers (3)

Stefan
Stefan

Reputation: 14863

Yes the, data attribute selected would be beetter, then setting a CSS class for you jQuery selector.

Here is an example using the data-X attribute selecotr

HTML:

<p data-test="">Hello</p>

JS:

var text = $('[data-test]').text(); 
alert(text);

Fiddle

Upvotes: 1

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174997

Yes, it's perfectly acceptable to use classnames like that.

A class name is used to designate similar groups of elements. So that fits right in.

Upvotes: 0

Jan Hančič
Jan Hančič

Reputation: 53929

Yes you can use "dummy" class names, that don't actually have any styles defined in the CSS.

And the correct seletor would be $(".identify1")

Upvotes: 1

Related Questions