Vivek Vermani
Vivek Vermani

Reputation: 2014

Javascript - How to achieve show hide for an HTML section on basis of a value matching any of the tags

I want to achieve something like this

<div id="121" tags="single,multi,binary">HTML Section</div>

<div id="122" tags="single,binary">HTML Section 2</div>

<div id="123" tags="binary">HTML Section 2</div>

Within javascript if i have a value "binary" it should show all three.

If I have value for a variable as single it should display 1st and second.

I know how to show hide the div. I just want to know if i can add user defined attribute to div and pull its value to match. If not , is there an alternate design to achieve this.

Upvotes: 1

Views: 127

Answers (2)

GregT
GregT

Reputation: 36

You could change tags to classes, -> class="single multi binary"

Then use class selectors in jquery. -> $('.single').each(function(){ /* Code Here */ });

Then use jquery addClass and removeClass to push and pull attributes. -> $(element).addClass('hex');

Upvotes: 1

Amit Joki
Amit Joki

Reputation: 59252

You can do this:

$('[tags*=single]')

But I would advice you to use data-tags

*= is contains prefix selector.

With pure javascript:

document.querySelectorAll("[tags*=single]") // returns NodeList

Upvotes: 2

Related Questions