We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

jQuery: html content of first ul in var

hankknight
hankknight asked
on
Medium Priority
464 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

Comment
Watch Question

Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
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
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Thanks for the points!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.