for example I have this code
<script>
$(document).ready(function () {
$('span').each(function () {
$(this).html('<div></div>') ;
if ( $(this).attr('id') == 'W0' ) { $( this > div ?!!! ).text('0') }
if ( $(this).attr('id') == 'W1' ) { $( this > div ?!!! ).text('1') }
if ( $(this).attr('id') == 'W2' ) { $( this > div ?!!! ).text('2') }
});
});
</script>
<span id="W0"></span>
<span id="W1"></span>
<span id="W2"></span>
But $( this > div )
or $( this ' > div ' )
are wrong selector & doesn't work
So what do u guys suggest I should do ?
You can pass a context to jQuery along with the selector
or use children() like
But your solution can be done as
You can use it as follow:
Docs: http://api.jquery.com/child-selector/
OR
For direct child elements, you can use
children
:Docs: http://api.jquery.com/children/
OR
Using
find
Docs: http://api.jquery.com/find/
Demo