Javascript/JQuery to make Multidimentional Arrary of all DOM Elements i defined? -
lets have following dom
s on form:
<body> <input id="input_1" class="post" type="text" value="abc" /> <input id="input_2" class="xxxx" type="text" value="xyz" /> <!--to ignore--> <input id="input_3" class="post" type="checkbox" checked="checked" /> <input id="input_4" class="post" type="radio" checked="checked" /> <select id="input_5" class="post"> <option value="1">one</option> <option value="2" selected="selected">two</option> </select> <input id="input_6" class="xxxx" type="checkbox" /> <!--to ignore--> </body>
how can value array of class="post"
elements?
multidimentional array
in way like:
post_elements[0]["input_1"] = "abc"; post_elements[1]["input_3"] = 1; post_elements[2]["input_4"] = 1; post_elements[3]["input_5"] = "two";
- how construct kind of
array
of specific classclass="post"
please?
you don't need 2 dimension array, index of array indicate 0
, 1
, 2
, etc.
array-like jquery object
you can array-like object allows access matching elements already.
var posts = $('.post'); alert(posts[0].id); // input_1 alert(posts[0].value); // abc
array-like pure js lookup
you can use pure javascript method getelementsbyclassname
grab elements (though jquery functions won't work without casting $()
)
var posts = document.getelementsbyclassname('post'); alert(posts[0].id); // input_1 alert(posts[0].value); // abc
array
alernatively construct actual array (not array-like object) can this:
var posts = []; $('.post').each(function () { posts.push(this); }); alert(posts[0].id); // input_1 alert(posts[0].value); // abc
Comments
Post a Comment