[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

find a child element inside an element without tracing

Posted on 2010-09-05
6
Medium Priority
?
437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 2

Expert Comment

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

Accepted Solution

by:
SandyAgo earned 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
The viewer will learn how to dynamically set the form action using jQuery.
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)

656 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