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
Solved

find a child element inside an element without tracing

Posted on 2010-09-05
6
433 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…

809 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