admin管理员组

文章数量:1425746

I want to iterate over a list of DOM elements (check boxes) and keep going as long as this list defined. The elements are 'c1r1', 'c1r2', 'c1r3', etc. Once I hit an undefined one, I stop. The problem seems to be using typeof with DOM elements.

Here's the offending code:

function domIsDefined(idString){
    alert(idString);
    var isItDefined = (typeof $(idString) != 'undefined');
    alert(isItDefined);
    return isItDefined;
}
...
for(i=1; domIsDefined('c1r' + i); i++){
    if($('c1r' + i).checked==true){
        // do stuff
    }
}

The crux of the problem is this line:

var isItDefined = (typeof $(idString) != 'undefined');

The problem, as I found out, is that typeof $(idString) always returns object, whether it is defined or not. Is there any good way to do this sort of thing? I guess I'll put in a try catch and check the .checked property early, but that feels disgusting.

I want to iterate over a list of DOM elements (check boxes) and keep going as long as this list defined. The elements are 'c1r1', 'c1r2', 'c1r3', etc. Once I hit an undefined one, I stop. The problem seems to be using typeof with DOM elements.

Here's the offending code:

function domIsDefined(idString){
    alert(idString);
    var isItDefined = (typeof $(idString) != 'undefined');
    alert(isItDefined);
    return isItDefined;
}
...
for(i=1; domIsDefined('c1r' + i); i++){
    if($('c1r' + i).checked==true){
        // do stuff
    }
}

The crux of the problem is this line:

var isItDefined = (typeof $(idString) != 'undefined');

The problem, as I found out, is that typeof $(idString) always returns object, whether it is defined or not. Is there any good way to do this sort of thing? I guess I'll put in a try catch and check the .checked property early, but that feels disgusting.

Share Improve this question edited Dec 29, 2011 at 16:41 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Nov 16, 2010 at 17:51 jtpereydajtpereyda 7,41511 gold badges56 silver badges84 bronze badges 3
  • 1 is there any specific reason you can't get all the checboxes using single DOM method, i.e. getElementsByTagName('input') and iterate over the collection of nodes? Or, if you use jQuery, just $(':checked').each( function(){ /*do stuff*/ }); – pawel Commented Nov 16, 2010 at 17:58
  • This is using prototype, sorry for the confusion. I should have clarified that. – jtpereyda Commented Nov 16, 2010 at 18:02
  • Also sorry about the IE in the title... I had thought it was IE only, but my Firebug console was just turned off. – jtpereyda Commented Nov 16, 2010 at 18:03
Add a ment  | 

5 Answers 5

Reset to default 5
function domIsDefined(idString){
    return !!document.getElementById(idString);
}

Check the length of the array. jQuery always returns a jquery instance, with an array of the matched elements.

$(idString).length > 0
if($("#id").length){}

jQuery always returns an object (an array). If the element with that ID is not found, then the length of the returned array will be 0.

var isItDefined = ($(idString).length > 0);

Update: for prototype, you should check for null to see if the object is found

var isItDefined = ($(idString) !== null);

$(), in jQuery, will always return an object. Try this instead:

var isItDefined = (typeof document.getElementById(idString) != 'undefined');

本文标签: Using Javascript39s typeof on DOM elements to check undefined (IE problem)Stack Overflow