Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

jQuery: First image in var

Posted on 2011-05-04
3
Medium Priority
?
328 Views
Last Modified: 2012-05-11
I want the first image of var h to be alerted.  For example, there should be an alert with the following:
<img src="1" alt="1" />
<!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>Website</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>

</head>
<body>

<h1>I want the first image of var h to be alerted.</h1>


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

var h = '<img src="1" alt="1" /><img src="2" alt="2" /><img src="3" alt="3" />';

alert( $(h).first() );
alert( 'Should alert <img src="1" alt="1" />' );

/*]]>*/
</script>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 1200 total points
ID: 35692657

<!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>Website</title>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
// http://yelotofu.com/2008/08/jquery-outerhtml/
jQuery.fn.outerHTML = function(s) {
return (s) ? this.before(s).remove() : jQuery("<p>").append(this.eq(0).clone()).html();
}

$(function(){
	var h = '<img src="1" alt="1" /><img src="2" alt="2" /><img src="3" alt="3" />';
	alert( $('img:eq(0)', $('<p>').append(h)).outerHTML() );
});


</script>
</head>
<body>

<h1>I want the first image of var h to be alerted.</h1>




</body>
</html>

Open in new window

0
 
LVL 61

Expert Comment

by:HainKurt
ID: 35692738
use

alert("<" + h.split("<")[1]);
0
 
LVL 61

Assisted Solution

by:HainKurt
HainKurt earned 800 total points
ID: 35692752
or this

alert(h.split(">")[0]+">");
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one 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, 13 hours left to enroll

580 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