SHEKHAR SHETE SHEKHAR SHETE - 7 months ago 29
Javascript Question

Iterate through a class of specific div which is inside another div

I have a 2 divs with with inner controls having class name '.news-member-apps'. on same page. Now, i want to Loop through the class name of specific div say i want to loop through #div1 which has class name '.news-member-apps' and not the other #div2. and get the value.

Note: Here i am copying the previous page DIV content to current page div i.e eg: in #PrevSelectedNews and looping through it.

var id = document.getElementById('<%=HiddenFieldNewsID.ClientID%>');
$('#PrevSelectedNews').load("/Default.aspx #ScrollerDiv", function () {
alert(id.value);
alert('Load was performed.' + $('#PrevSelectedNews'));
$(".news-member-apps").each(function (k) {
var NEWSID = $(this).attr("newsid");
var ID = $(this).attr("id");
if (NEWSID == id.value) {

$("#" + ID).appendTo("#SelectedNews");
alert($("#" + ID) + "Found! Appending");

}

});

});


The #PrevSelectedNews contains all news items from previous page. Now just i want to loop through #PrevSelectedNews only where inner controls class has name '.news-member-apps'. If the HiddenFiledNewID.value matched with any of the control having class name 'PrevSelectedNews' then get the id of foind control.
Help Appreciated!
Thanks!

Answer

Use selector:

$('#PrevSelectedNews').find('.news-member-apps').each(function() { ... })

Here it will find element with ID 'PrevSelectedNews' and than will iterate through child elements with class 'news-member-apps'

Jquery selectors reference: http://api.jquery.com/category/selectors/

Comments