3

I have to check if an element was selected. The only way I can do it is to watch if an additional css class "selected" was added to this element.

So, my element looks like:

<div class="b-wide-option" data-bind="css: { selected: IsSelected }, click: Select, attr: { id: Id }, event: { mouseover: OnMouseOver, mouseout: OnMouseOut, touchstart: OnTouchClick }" id="2">

When I select it it looks like:

<div class="b-wide-option selected" data-bind="css: { selected: IsSelected }, click: Select, attr: { id: Id }, event: { mouseover: OnMouseOver, mouseout: OnMouseOut, touchstart: OnTouchClick }" id="2">

The css class "selected" is added. And I need to check if it was added or not. I use the next code:

string classes = element.GetAttribute("class");

But unfortunately it returns only the first class "b-wide-option" and don't return the second which I actually need.

2
  • Is the issue a timing issue? e.g. if you were to extract the class attribute 1 second later would you expect it to be there? Commented Oct 14, 2014 at 12:43
  • No, it's a static element. Once I selected it the class "selected" is present Commented Oct 14, 2014 at 12:45

4 Answers 4

10

Use this

       element.getAttribute("className");

Hope this will help you to solve problem

Sign up to request clarification or add additional context in comments.

3 Comments

No, he has only word "class" not "className"
Do I have an atribute with the name of "className"?
use this element.getAttribute("className"); instead of element.getAttribute("class");
1

I'm so sorry guys! I've just checked another element, my fault! The simple code as element.getAttribute("className"); and element.getAttribute("class"); works perfect, indeed!)

I wish moderator delited this question, I"m sorry.

Comments

0

You can use

getElementsByClassName('foo')

or u can also use jquery

var className = $('.b-wide-option').attr('class');

or u can also use .hasClass() function.

var className = $('.b-wide-option').attr('class');
if($('.b-wide-option').hasClass('selected')){
//write your code
}

Comments

0

You can get the class names by this:

element.Attributes["class"].ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.