Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - get elements by tag within a specific div?

I'm trying to get all input elements within a certain div, but it seems to return all inputs on any given page... is there a way to do this?

if($('#umi-form')) {
          var inputs = document.getElementsByTagName('input');
}
like image 670
newbie-shame Avatar asked Dec 14 '10 19:12

newbie-shame


2 Answers

It can be done like this:

var $inputs = $('#umi-form input');
like image 106
treeface Avatar answered Sep 18 '22 15:09

treeface


Your if() test will always evaluate true. You should use the length property in the if().

var uform = $('#umi-form');
if(uform.length) {
    var inputs = uform.find('input');
}

If you were hoping to get a nodeList instead of a jQuery object, do this:

var uform = $('#umi-form');
if(uform[0]) {
    var inputs = uform[0].getElementsByTagName('input');
}
like image 26
user113716 Avatar answered Sep 19 '22 15:09

user113716