最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Hide or show all divs for a certain value of a data attribute - Stack Overflow

programmeradmin1浏览0评论

I'm trying to use HTML5 valid markup here, so instead of adding to values to class I want to use the data-*="" to show/hide certain divs.

<div class="randomclass" data-chattingto="cheesecake"></div>
<div class="randomclass" data-chattingto="milkshake"></div>
<div class="randomclass" data-chattingto="cheesecake"></div>
<div class="randomclass" data-chattingto="milkshake"></div>

I can get the value of data-chattingto like so:

$("div").data("chattingto");

and I can hide/show a div with a certain class like so:

$(".randomclass").hide();

But how do I hide all divs with the data-chattingto value of cheesecake?

Thank you very much.

I'm trying to use HTML5 valid markup here, so instead of adding to values to class I want to use the data-*="" to show/hide certain divs.

<div class="randomclass" data-chattingto="cheesecake"></div>
<div class="randomclass" data-chattingto="milkshake"></div>
<div class="randomclass" data-chattingto="cheesecake"></div>
<div class="randomclass" data-chattingto="milkshake"></div>

I can get the value of data-chattingto like so:

$("div").data("chattingto");

and I can hide/show a div with a certain class like so:

$(".randomclass").hide();

But how do I hide all divs with the data-chattingto value of cheesecake?

Thank you very much.

Share Improve this question asked Aug 10, 2012 at 21:04 Seb123Seb123 4911 gold badge7 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

Simply use attribute-equals notation:

$('div[data-chattingto="cheesecake"]').hide();

And a JS Fiddle demo, kindly provided by CrunchyV. References:

  • [attribute="value"] selector.

That would be:

$('div[data-chattingto = "cheesecake"]').show();

And

$('div[data-chattingto = "cheesecake"]').hide();
发布评论

评论列表(0)

  1. 暂无评论