Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery find if the page contains specific id?

Is there some one can help me about this? If the page contains id = "item1" executes #home.hide(); I am really frustrated about this. My code:

<tr>
<td id = "item1">
</tr>

if($("body:has(#item1)")){
$('#home').hide();
}
like image 497
Eric Avatar asked Oct 05 '11 06:10

Eric


2 Answers

If what you're trying to do is to execute $('#home').hide(); only if the #item1 object is present, then you would do that like this:

if ($("#item1").length > 0) {
    $('#home').hide();
}

There is no need for checking if #item1 is in body since that's the only place it can be. You can simply just check for #item1 since ids must be unique.

You could even resort to plain JS for the condition as an illustration of how simple it is:

if (document.getElementById("item1")) {
    $('#home').hide();
}

If that isn't what you're trying to do, then please clarify your question further.

like image 169
jfriend00 Avatar answered Oct 06 '22 04:10

jfriend00


All you need is:

<script type="text/javascript">
$(function() {
    if($("#item1").length) {
        $('#home').hide();
    }
});
</script>
like image 36
Jeff Avatar answered Oct 06 '22 05:10

Jeff