unix_user
unix_user

Reputation: 309

How to provide elements/tags dynamically inside the find() in jquery

I have created several div with id's such as window1, window2 and so on. Now all I want to is find the

tag from these above created divs. I am doing this inside the for loop but it is not working for me. Here is what I am doing

        for(connectWindow=1;connectWindow<=xmlLength;connectWindow++)
        {

                //look for the to tag inside the html
                var windo = "window"+connectWindow;
                var to = "to"+connectWindow;
                alert("Making connections" + windo +to)
                //$("div#windo").find('strong#to')(function())
                $("div#windo").find('p#to').each(function(){
                    alert("@@@@@@@@@@@@@@@@@@@@");
                    var name = $(this).text();
                    //display_function(name,country);
                    alert("Name is :::"+name);

                });
          }

Please let me know where I am going wrong. Also please let me know if there is any solution in JavaScript either. Thanks !

Upvotes: 1

Views: 57

Answers (2)

wirey00
wirey00

Reputation: 33661

You need to do it like this

 $("div#" + windo).find('p#' + to).each(function(){ // <-- this uses your variable
          alert("@@@@@@@@@@@@@@@@@@@@");
          var name = $(this).text();
          //display_function(name,country);
          alert("Name is :::"+name);
  });

Your code looks for an id="window" and id="to" instead of your variable

$("div#windo").find('p#to') 

You really can just do it by ID since you are using the #(id selector)

$("#" + windo).find('#' + to)

Upvotes: 1

Hamish
Hamish

Reputation: 23316

Well, you need to actually use the variables:

$("div#" + windo).find('p#' + to).each(function(){

By the way - jQuery is written in JavaScript. If you're using jQuery, you're using JavaScript.

Upvotes: 1

Related Questions