Header AD

Parents and Children Selectorswith jQuery

J Query Tips On Parents and Children Selectors

The following code shows two <div> elements, each with the same content inside:

<!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"><html>

<head>

<title>My Test Page</title>

<script type=”text/javascript” src=”scripts/jquery-1.6.4.min.js”></script>

<script type=”text/javascript”>$(document).ready(function(){

// code
$(‘div:first-child’).text(‘Change me.’);
//code

});

</script>

</head>

<body>

<div id=”myfirstdiv”>

<strong class=”changemytext”>your name goes here</strong>

<p class=”changemytext”>text here<p>

<strong>another name goes here</strong>

<p>More text here<p>

</div>

<div id=”myseconddiv”>

<strong class=”changemytext”>more name</strong>

<p class=”changemytext”>more text<p>

<strong>another name</strong>

<p>More text<p>

</div>

</body>

</html>




select elements based on their parents or children, try these selectors:



first-child: Selects the first child element. The following code selects the first child of the first <div> and changes the text of the selected element:

$(‘div:first-child’).text(‘Change me.’);


last-child: Selects the last child element. The following code selects the last child of the second <div> and changes the text of the selected element:

$(‘div:last-child’).text(‘Change me.’);


child: Selects the child element of the parent element. This code changes the text of every <strong> element that is a child of a <div> element


$(‘div > strong’).text(‘Change me.’);


You can find the complete list of selectors at http://api.jquery.com/category/selectors/.


Meta tag generator 4 seo
If else statement
Increase traffic your blogs
Popularity by massage
Tips 4 blog posting



Parents and Children Selectorswith jQuery Parents and Children Selectorswith jQuery Reviewed by Unknown on 4:09 PM Rating: 5

No comments

Post AD