Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find an element by style attribute?

I am having trouble retrieving elements by style attribute. I have a ul with several li elements and I would like to find the one with the style atribute having a value display:list-item. I have tried to approach this in several ways, including the following, but I keep getting an empty object. I have no problem with a title attribute. Not sure what I am doing wrong.

$('li[style*=display:list-item]')

HTML:

<ul class="bjqs" style="height: 200px; width: 100%;">
    <li class="bjqs-slide" style="height: 200px; width: 100%; display: list-item;">
like image 443
user2232681 Avatar asked Oct 24 '13 21:10

user2232681


2 Answers

Your selector is white-space sensitive, given that you have white-space in the attribute, you'll need that in your selector as well:

$('li[style*="display: list-item"]')

The easiest way, which reduces the problems of precisely matching a string, if you're filtering elements according to a particular CSS property, is to use filter():

$('li').filter(function(){
    return $(this).css('display') === 'list-item';
});

References:

  • filter().
like image 74
David Thomas Avatar answered Sep 29 '22 08:09

David Thomas


Two problems:

  1. In your HTML there is a space between "display:" and "list-item".
  2. Your selector needs to be wrapped with quotations "".

Simply change your selector to include this space and wrap it in quotes:

$('li[style*="display: list-item"]')

JSFiddle demo.

like image 41
James Donnelly Avatar answered Sep 29 '22 08:09

James Donnelly