?
Solved

jQuery: html content of first ul in var

Posted on 2011-04-29
4
Medium Priority
?
453 Views
Last Modified: 2012-05-11
I want to alert the HTML inside the first ul tag.
The first set works but the second set does not work.
<!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>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<body>

<h1>I want to alert the HTML inside the first ul tag.</h1>
<h1>The first set works but the second set does not work.</h1>


<script type="text/javascript">
/*<![CDATA[*/

var data = '<div><ul><li><a href="#" class="item">Foobar</a> | <a href="#">12 Live</a> | <a href="#">3 in process</a> | <a href="#">2 inactive</a> </li> <li> <a href="#" class="item">Zooph</a> | <a href="#">32 Live</a> | <a href="#">12 in process</a> | <a href="#">0 inactive</a> </li> <li> <a href="#" class="item">Quack</a> | <a href="#">11 Live</a>  | <a href="#">21 in process</a> | <a href="#">4 inactive</a></li></ul></div>';
var oSet = $(data).find('ul').first().html();
alert(oSet);

var data = '<div>xyz</div><ul><li><a href="#" class="item">Foobar</a> | <a href="#">12 Live</a> | <a href="#">3 in process</a> | <a href="#">2 inactive</a> </li> <li> <a href="#" class="item">Zooph</a> | <a href="#">32 Live</a> | <a href="#">12 in process</a> | <a href="#">0 inactive</a> </li> <li> <a href="#" class="item">Quack</a> | <a href="#">11 Live</a>  | <a href="#">21 in process</a> | <a href="#">4 inactive</a></li></ul>';
var oSet = $(data).find('ul').first().html();
alert(oSet);

alert('The second set should not be null.  It should be simular to the first set.');

/*]]>*/
</script>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
  • 2
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 1800 total points
ID: 35490640
Perhaps there's a better option (set a new container around data), but check this :
<!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>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<body>

<h1>I want to alert the HTML inside the first ul tag.</h1>
<h1>The first set works but the second set does not work.</h1>


<script type="text/javascript">
/*<![CDATA[*/

//var data = '<div><ul><li><a href="#" class="item">Foobar</a> | <a href="#">12 Live</a> | <a href="#">3 in process</a> | <a href="#">2 inactive</a> </li> <li> <a href="#" class="item">Zooph</a> | <a href="#">32 Live</a> | <a href="#">12 in process</a> | <a href="#">0 inactive</a> </li> <li> <a href="#" class="item">Quack</a> | <a href="#">11 Live</a>  | <a href="#">21 in process</a> | <a href="#">4 inactive</a></li></ul></div>';
//var oSet = $(data).find('ul').first().html();
//alert(oSet);

data = '<div>xyz</div><ul><li><a href="#" class="item">Foobar</a> | <a href="#">12 Live</a> | <a href="#">3 in process</a> | <a href="#">2 inactive</a> </li> <li> <a href="#" class="item">Zooph</a> | <a href="#">32 Live</a> | <a href="#">12 in process</a> | <a href="#">0 inactive</a> </li> <li> <a href="#" class="item">Quack</a> | <a href="#">11 Live</a>  | <a href="#">21 in process</a> | <a href="#">4 inactive</a></li></ul>';
var oSet = $("<container>" + data + "</container>").find("ul").first().html();
alert(oSet);

//alert('The second set should not be null.  It should be simular to the first set.');

/*]]>*/
</script>

</body>
</html>

Open in new window

0
 
LVL 10

Assisted Solution

by:khan_webguru
khan_webguru earned 200 total points
ID: 35491839
Why you are declaring same variable again and again ????

 
var data = '<div><ul><li><a href="#" class="item">Foobar</a> | <a href="#">12 Live</a> | <a href="#">3 in process</a> | <a href="#">2 inactive</a> </li> <li> <a href="#" class="item">Zooph</a> | <a href="#">32 Live</a> | <a href="#">12 in process</a> | <a href="#">0 inactive</a> </li> <li> <a href="#" class="item">Quack</a> | <a href="#">11 Live</a>  | <a href="#">21 in process</a> | <a href="#">4 inactive</a></li></ul></div>';
var oSet = $(data).find('ul').first().html();
alert(oSet);

var data = '<div>xyz</div><ul><li><a href="#" class="item">Foobar</a> | <a href="#">12 Live</a> | <a href="#">3 in process</a> | <a href="#">2 inactive</a> </li> <li> <a href="#" class="item">Zooph</a> | <a href="#">32 Live</a> | <a href="#">12 in process</a> | <a href="#">0 inactive</a> </li> <li> <a href="#" class="item">Quack</a> | <a href="#">11 Live</a>  | <a href="#">21 in process</a> | <a href="#">4 inactive</a></li></ul>';
var oSet = $(data).find('ul').first().html();
alert(oSet);

Open in new window


Just change the variables name that should be unique for better understanding

Thanks,
0
 
LVL 10

Expert Comment

by:khan_webguru
ID: 35491927
Try this

 
<!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>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<body>

<h1>I want to alert the HTML inside the first ul tag.</h1>
<h1>The first set works but the second set does not work.</h1>


<script type="text/javascript">
/*<![CDATA[*/

var data = '<div><ul><li><a href="#" class="item">Foobar</a> | <a href="#">12 Live</a> | <a href="#">3 in process</a> | <a href="#">2 inactive</a> </li> <li> <a href="#" class="item">Zooph</a> | <a href="#">32 Live</a> | <a href="#">12 in process</a> | <a href="#">0 inactive</a> </li> <li> <a href="#" class="item">Quack</a> | <a href="#">11 Live</a>  | <a href="#">21 in process</a> | <a href="#">4 inactive</a></li></ul></div>';
var oSet = $(data).find('ul').first().html();
alert(oSet);

var data1 = '<div><div>xyz</div><ul><li><a href="#" class="item">Foobar</a> | <a href="#">12 Live</a> | <a href="#">3 in process</a> | <a href="#">2 inactive</a> </li> <li> <a href="#" class="item">Zooph</a> | <a href="#">32 Live</a> | <a href="#">12 in process</a> | <a href="#">0 inactive</a> </li> <li> <a href="#" class="item">Quack</a> | <a href="#">11 Live</a>  | <a href="#">21 in process</a> | <a href="#">4 inactive</a></li></ul></div>';
var oSet1 = $(data1).find('ul').first().html();
alert(oSet1);

alert('The second set should not be null.  It should be simular to the first set.');

/*]]>*/
</script>

</body>
</html>

Open in new window


Thanks
0
 
LVL 83

Expert Comment

by:leakim971
ID: 35491942
Thanks for the points!
0

Featured Post

Technology Partners: 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

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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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)
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

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