Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 817
SHEKHAR SHETE Avatar asked Apr 22 '13 07:04

SHEKHAR SHETE


People also ask

How to get all elements of a specific class inside div?

In this article, we will find how to get all elements of a specific class inside an HTML div tag. To do this we will use HTML DOM querySelectorAll () method. This method of HTML DOM (document object model) returns all elements in the document that matches a specified CSS selector (s). The syntax to use this method is as follows.

How to iterate through child elements of a Div using jQuery?

How to iterate through child elements of a div using jQuery ? jQuery Selector can be used to find (select) HTML elements from the DOM. Once an element is selected, the jQuery children () method is called to find all the child elements of the selected element.

How to loop through all div elements in jQuery?

The jQuery code in the following example will loop through each DIV elements and highlight the background of only those elements which are empty. Let's try it out: In the above example $ (this) represent the current DIV element in the loop. You can attach jQuery methods directly to $ (this) to perform manipulations.

How to iterate through all elements in a HTML array?

Since getElementsByTagName () returns an array, you can use a for loop for each of the elements. This will push the innerHTML of each of the elements into the divArray variable and iterate through them. Thanks for contributing an answer to Stack Overflow!


1 Answers

Use this selector:

$('#PrevSelectedNews').find('.news-member-apps').each(function() { 
    // this - reference to each .news-member-apps item
})

It finds element with ID 'PrevSelectedNews' and than iterates through child elements with class 'news-member-apps'.

JQuery selector docs here.

like image 65
Andrei Avatar answered Sep 27 '22 21:09

Andrei