Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a DIV id exists with JQuery

Using Javascript I'm trying to test whether or not a DIV with the id "test_div" exists in the document.

I'm using

if (getElementById("test_div"))

in the below script to check if the DIV exists in the document. However it does not work. I don't have very much experience with Javascript; but from the research I have done it seems like my problem might have something to do with the nested function.

The script does work if I remove the

if (getElementById("test_div"))

Does anyone know how to check if a DIV with the id "test_div" exists in the document from within the function as shown below?

<div id="test_div"></div>

<script>
function loadInfo(){
var req = new Request({
    method:'get',
    url:'getinfo.php,
    noCache: true,
    onRequest: function(){

        if (getElementById("test_div")) {
            $('test_div').set('html', 'loading data');
        }

    },  
    onComplete:function(responseText, responseHtml){
        if (JSON.decode(responseText) != null){
            var data = JSON.decode(responseText);

            if (getElementById("test_div")) {
                $('test_div').set('html', data['test_div']);
            }

        }   
    },
    onFailure: function(){

        if (getElementById("test_div")) {
            $('test_div').set('html', '-');
        }

    }           
}).send();  
}
window.addEvent('domready', function(){
loadInfo();
});
</script>
like image 384
Marcus Avatar asked Nov 13 '10 03:11

Marcus


People also ask

How do you check if the DIV is already exists?

$(document). ready(function() { if ($('#DivID'). length){ alert('Found with Length'); } if ($('#DivID'). length > 0 ) { alert('Found with Length bigger then Zero'); } if ($('#DivID') !=

How do you find if element with specific ID exists or not?

Approach 1: First, we will use document. getElementById() to get the ID and store the ID into a variable. Then compare the element (variable that store ID) with 'null' and identify whether the element exists or not.

How do you check class is exists in jQuery?

jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".

Is visible in jQuery?

The :visible selector in jQuery is used to select every element which is currently visible. It works upon the visible elements. The elements that are consuming space in the document are considered visible elements. The height and width of visible elements are larger than 0.


2 Answers

You would have to do document.getElementById(id). But seeing how you are using jquery - you could also do $(id).length > 0 to check if the element exists

like image 119
Aishwar Avatar answered Sep 28 '22 08:09

Aishwar


Your case is incorrect in your sample code. You should be invoking getElementById, with a lowercase d. Javascript is case sensitive.

If you're using jquery, make sure to prefix id with a #, like so $("#test_div"), so jquery knows you want to query for an element id.

like image 31
Bob Black Avatar answered Sep 28 '22 09:09

Bob Black