-2

Possible Duplicate:
Get Class List for Element with jQuery

how can i get the class value of a span inside a div?

<div id="Check">

<span class="checked">

<input id="box" name="INbox" type="checkbox" value="1" />1<br>

<span>

</div>

how to get the value of class="checked" from on div="Check"?

and use it like this

<script>
var span = $('div#Outbound').find('span').attr('class');
if ( span != "checked"){

}
</script>
Community
  • 1
  • 1
telexper
  • 2,381
  • 8
  • 37
  • 66
  • What are you trying to do? Why not add the class to the `.find()` selector? And what do you expect `.value()` to do? There's no such jQuery method, and `.val()` doesn't work on `span` elements. – I Hate Lazy Sep 30 '12 at 21:08
  • 1
    I recommend to read the documentation first before you use a method, so that you don't waste time trying things. You would also found out that there is no method with name `.value`: http://api.jquery.com/?ns0=1&s=.value – Felix Kling Sep 30 '12 at 21:08
  • Wait, now you want to do something when it does not have the class? I think you should describe what you're ultimately trying to do. This code gives me a strong sense that you're taking the wrong approach to whatever the actual problem is. – I Hate Lazy Sep 30 '12 at 21:16

3 Answers3

2

Use .attr():

var spanClass = $('div#Outbound').find('span').attr('class');

Or if you just want to determine if the span has a class named checked, use .hasClass():

var hasCheckedClass = $('div#Outbound').find('span').hasClass('class');
João Silva
  • 89,303
  • 29
  • 152
  • 158
2

Without need for jQuery:

var span = document.getElementById('Check').getElementsByTagName('span')[0].className;

Or, if the format of the HTML is predictably "the span is the first element inside the div" then you can cheat a little:

var span = document.getElementById('Check').children[0].className;

To check if the class checked is on the span, you can do this:

if( span.match(/\bchecked\b/))
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

You can use jQuery hasClass

 <script>

    if ( !$('div#Outbound').find('span').hasClass( "checked" )){

    }
    </script>
Anoop
  • 23,044
  • 10
  • 62
  • 76