Solved

find a child element inside an element without tracing

Posted on 2010-09-05
6
432 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
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)

810 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