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
5 Answers
Reset to default 5function 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
版权声明:本文标题:Using Javascript's typeof on DOM elements to check undefined (IE problem) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745367526a2655587.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论