Solved

find a child element inside an element without tracing

Posted on 2010-09-05
6
431 Views
Last Modified: 2012-05-10
easier way of finding an element inside another element.
say:

<div id="test">
     <div id="desc1" class="description1">
            <div class="name"></div>
            <div class="addressInfo">
                    <div class="address"></div>
                    <div class="city"></city>
           </div>
     </div>
     <div id="desc2" class="description2">
            <div class="name"></div>
            <div class="addressInfo">
                    <div class="address"></div>
                    <div class="city"></city>
           </div>
     </div>
</div>

say in my function i have var desc = $('desc1') or desc = $('desc2')
i passed this element to a function:
setDescription = function(desc) {
      //set address here how? based on desc?
     //this has to be dynamic based on desc value
}

i want  set the address value of either 1 or 2, since i know desc (desc1 or desc2)

how to do that with selector? i want do it one by one , set desc1 address then  desc2 address.

thanks

0
Comment
Question by:johndenny
6 Comments
 
LVL 2

Expert Comment

by:SandyAgo
ID: 33608552
$('#desc1').find('.addresinfo');
0
 
LVL 2

Accepted Solution

by:
SandyAgo earned 500 total points
ID: 33608561
similarly you can simply ask for

$('#desc2').find('.addressinfo');

so putting it together:

setDescription = function(desc) {
      //set address here how? based on desc?
      //this has to be dynamic based on desc value

      if (desc =="your required value for desc1")  {
            $('#desc1').find('.addressinfo');
      }
      else if (desc == "your required val for desc2")  {
            $('#desc2').find('.addressinfo');
      }

}


else
0
 
LVL 2

Expert Comment

by:SandyAgo
ID: 33608567
oops, you also need use .html(desc) on the end of the statement to set the value:

      $('#desc1').find('.addressinfo').html(desc);
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 82

Expert Comment

by:leakim971
ID: 33608631
What about : $("#" + desc)

example to set innerHTML of "name" :


setDescription = function(desc) {
   $("#" + desc).children("name").text("A NAME");
}

or 

setDescription = function(desc) {
   $("name", "#" + desc).text("A NAME");
}

Open in new window

0
 
LVL 22

Expert Comment

by:kadaba
ID: 33608809
Use selectors
$("#" + desc + " > div.addressInfo > div.address").html();


Its much easier to go  through parent child relationship this way.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
		setAddress("desc1","Address 1");
		setAddress("desc2","Address 2");
	});
	
	function setAddress(desc,address)
	{
		$("#" + desc + " > div.addressInfo > div.address").html(address);
	}
</script>
</head>
<body>
<div id="test">
     <div id="desc1" class="description1">
            <div class="name">
			</div>
            <div class="addressInfo">
				<div class="address">
				</div>
				<div class="city">
				</div>
           </div>
     </div>
     <div id="desc2" class="description2">
            <div class="name">
			</div>
            <div class="addressInfo">
                    <div class="address">
					</div>
                    <div class="city">
					</div>
           </div>
     </div>
</div>
</body>
</html>

Open in new window

0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 33609333
since 'desc' is the context, you can use it as the context...
function setAddress(desc, address, city)

    $(".address", desc).text(address);

    $(".city", desc).text(city);

}



// Usage : setAddress($("#desc1"), "10th street", "London");

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can I make a css class read only with jquery? 2 37
JS Plugin 4 29
CSS Position not returning 2 46
Full Screen problem when auto scale IFRAME 2 61
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now