Jquery find if page contains specific id?

Is there anyone who can help me? If the page contains id = "item1" does # home.hide (); I am very upset about this. My code is:

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

if($("body:has(#item1)")){
$('#home').hide();
}
+5
source share
4 answers

If what you are trying to do is execute $('#home').hide();only if the object is #item1present, then you will do this:

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

There is no need to check if #item1in body, as this is the only place where this is possible. You can simply simply check #item1, as identifiers must be unique.

JS , :

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

, , , .

+8

, , :

<script type="text/javascript">
$(function() {
    if($("#item1").length) {
        $('#home').hide();
    }
});
</script>
+5

u can check it like

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

this will return true if such an element as'item1 'exists

+1
source
if($('#item1').length) $('#home').hide();

There are other ways, but the simplest.

0
source

All Articles