jQuery: html content of first ul in var

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

LVL 16
hankknightAsked:
Who is Participating?
 
leakim971Connect With a Mentor PluritechnicianCommented:
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
 
khan_webguruConnect With a Mentor Commented:
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
 
khan_webguruCommented:
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
 
leakim971PluritechnicianCommented:
Thanks for the points!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.