<div id="target">
<div id="test" att="test"></div>
</div>
$('target').haschild('#test')
should be true
$('target').haschild('#test[att="test"]')
should be true
$('target').haschild('#no')
should be false
Only $('target')
is available.
<div id="target">
<div id="test" att="test"></div>
</div>
$('target').haschild('#test')
should be true
$('target').haschild('#test[att="test"]')
should be true
$('target').haschild('#no')
should be false
Only $('target')
is available.
$('target').children('#test').length == 0
or
$('target > #test').length == 0
You can test a few ways, here is one:
if($("#target > #test").length){
//true
} else {
//false
}
If you play with jQuery plugins, you can actually use the syntax you use in your example:
$.fn.haschild = function(selector){
return ($("> " + selector, this).length > 0);
}
$(function(){
alert($('#target').haschild('#test')); // Alerts true
alert($('#target').haschild('#test[att="test"]')); // Alerts true
alert($('#target').haschild('#no')); // Alerts false
});