views:

54

answers:

5

Hello! In my script i'm looking for the DIV which has class .se and has style attribute display: block, and i need the DIV's ID.

My method is wrong :(

var usedse = $(".se"):has(css('display', 'block')).attr("id");

Could you help me please to fix this?

A: 

The syntax you are using is wrong, try this simple way instead:

if ($("div.se").css('display') == 'block') {
   var usedse = $(".se").attr('id');
   alert(usedse);
}
Sarfraz
`<div class="se" style="display:hidden" id="number1">1</div><div class="se" style="display:block" id="number2">2</div>` wouldn't ever match a `usedse` using your code. The [`.css()`](http://api.jquery.com/css) function returns the style of the first element. I would imagine the OP would want `usedse = 'number2'` not `undefined`
gnarf
@gnarf: I assume what he has posted. If he adds more details, we can modify our answers as per that.
Sarfraz
@sAc I assume what he has posted to... He is looking to find the `DIV` which has class `se` and the `style` attribute `display:block`, and he wants the div's ID. Your code fails to return that in my example HTML. It only checks that the first `div.se` has `display:block`, and if so, returns its ID.
gnarf
@gnarf - the OP uses `class="se"` and with just `var usedse` as the variable... what would you expect... unless the OP's want result arrays... I will not take out my up-vote...
Reigel
@Reigel: OP already clarified that in a comment under my answer.
jAndy
I think answers shouldn't upvoted/downvoted based on assumptions.
Sarfraz
@sAc - my -1 isn't on an assumption... and I at least commented my reasonong which was based on what I read in the question - "The ID of the `div.se` having style `display:block`" your logic doesn't seem to match the language the OP used, and it had a lot of upvotes over a much simpler solution that followed the logic I read.
gnarf
@gnarf: The ***having style display:block"*** doesn't say that it is inline not in that english at least. Let's see what OP had to decide. Bye
Sarfraz
A: 

I'd recommend this:

var usedse = $('.se:visible').attr('id');
    if(usedse) alert(usedse);
jAndy
@jAndy: The visible doesn't mean it is block, it could even be inline but still good short way of doing it.
Sarfraz
@sAc: I'm assuming (since nothing else was said) that the OP just wants to know if the element is visible or not. In that case, I like my code.
jAndy
Neet. Thanks. You saved hours for me. Yep, i've got 8 blocks with class `.se` and i want something to append at the shown block. So it's works fine.
neduddki
@jAndy: Right and i think OP seems to be happy with that :)
Sarfraz
+1 for simplicity that the point of checking `display:block` might be replaced with "not hidden"
gnarf
@downvoters: please enlighten me some day with a reason, since OP already confirmed this as helpful.
jAndy
I did not down-vote, but I think I can explain... `$('.se:visible').attr('id');` would return numerous if lots are found... then you are storing it with just one variable? :)
Reigel
@Reigel: well, I will not explain that further, but everyone who has a brain up there will realize (at least after OP clarified it here) that there *should* only be one visible element, otherwise, it's not part of the question and OP has to care about that himself (damn, I explained it anyway).
jAndy
@jAndy - lol. hoped they'll read that.. :)
Reigel
A: 

This probably be the best way to accomplish this :)

id = $(".se[style*='display:block']").attr("id");

But you would have to think of multiples here, as your using the classes as selectors it usually means theres multiple div elements that your trying to grab, if thats the case, try this way instead

$(".se[style*='display:block']").each(function(){
    //Here you can do what you want to each element
    // use `this` as a selector, for example
    id = this.attr('id');
});

Just a side note: You have to make sure that your selectors are selecting the correct item when it comes down to css, For example

style = $('element').attr('style'); will only return the value form the actual element such as <div style="display:block".

To get the style values from the element that have been set within css you will have to use the selector to filter them out.

http://jsfiddle.net/4jVC8/

You need to make sure you keep in mind that these are separate!

So heres another filter version.

$('div.se').filter(function() {
    return ( $(this).css('display') == 'block');
});
RobertPitt
I think this will not work... I guess might be `$(".se[style*='display:block']").attr("id");`, for `display` is not an attribute...
Reigel
I don't think this would work if `display` is set over a `css class`.
jAndy
yea you might be right, as display is not a dom attribute :(
RobertPitt
@jAndy - `display` is not an attribute... it will really not work..
Reigel
yes guys i changed it thanks, its 9am :( Ill get there after few more coffees :)
RobertPitt
@Robert - in your update, `id = $(this).attr('id');` would be nice if you'll just say `id = this.id;`, because you are currently inside that object... no need to wrap jQuery when getting attributes value.. ;)
Reigel
thats true aswell Reigel, I was trying not to confuse the OP, and keep it to what he knows. Im not here to improve the code, just show him a solution :)
RobertPitt
well, if possible for you, let's try to improve everybody's codes... it's for our sake also... good quality codes are better... :) well, just an opinion... cheers!
Reigel
I'm With you on that one!
RobertPitt
+1  A: 

You can write a lot of custom logic using .filter()... This will find all <div class='se'> and check that their CSS display property is set to block, returning the id of the first matched element.

var usedse = $("div.se").filter(function() { 
   return $(this).css('display') == 'block';
}).attr("id");
gnarf
A: 

You have a few options.

One thing you could do is the attribute selector which would return all elements with class se and display css property set:

$(".se [style*='display']").attr('id');

Or, you could use the .css() method and actually check the value of a certain css property:

var $possibles = $(".se"), id;
for ($el in $possibles)
    if ($el.css('display') === 'block') id = $el.attr('id');

Can I ask why you are doing this though? I feel like there must be a better way to look at the problem... maybe you should simply put a class on this element (obviously, this may not be possible depending on the exact problem)?

tjko
your just constantly overwriting the id variable
RobertPitt
@RoberPitt, not if we assume OP expects one ID to be returned. Otherwise, could just make `id` an array...
tjko