Solved

jQuery: span inside this

Posted on 2011-02-23
2
607 Views
Last Modified: 2012-05-11
How can I access a span inside this?

This does NOT work:
$(this + 'span').html()

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>

<style type="text/css">
span {
display: none;
}
</style>

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

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

$(document).ready(function() {

	$('.hoverInfo').hover(
	  function () {
		alert($(this + 'span').html() );
	  }
	);

});

// ]]>

</script>
</head>
<body>
<div>

<p>
<a class="hoverInfo">
One <span>1</span>
</a>
<p>
</p>
<a class="hoverInfo">
Two <span>2</span>
</a>
<p>
</p>
<a class="hoverInfo">
Three <span>3</span>
</a>
</p>

</div>
</body>
</html>

Open in new window

0
Comment
Question by:hankknight
2 Comments
 
LVL 42

Accepted Solution

by:
David S. earned 400 total points
ID: 34964054
You can't concatenate an object with a string like that.

jQuery allows you to specify the context with a second parameter like this:
$('span', this).html()

Open in new window

0
 
LVL 15

Assisted Solution

by:dirknibleck
dirknibleck earned 100 total points
ID: 34964060
$('.hoverInfo').hover(
          function () {
                alert($(this).children('span').html() );
          }
        );
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript waiting 14 69
JavaScript and Regular Expressions - How to implement date validation? 7 33
Message not shown 5 33
how can i remove string values 2 16
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now