Andromeda
Andromeda

Reputation: 12897

Jquery selector: How to select the content inside a given span class

<a class='leftpanel_anchor' tag='solutions' href='javascript:void(0)' > Solutions to <span class='leftpanel_keywords'>firstConResult</span></a>

I have an anchor like this. I want to select the content inside span class 'leftpanel_keywords'. how can I do that with jquery?

Upvotes: 0

Views: 409

Answers (7)

Jamie Rumbelow
Jamie Rumbelow

Reputation: 5095

You can use the CSS class selector to fetch the element. The class selector works like this:

$('.name_of_class')

This then returns a jQuery object of that DOM element, on which you can call various methods to do various things, such as animation, content manipulation and really anything that you can do with jQuery.

In your example here, you can fetch the innerHTML value (the element's content) by calling:

var content = $('.leftpanel_keywords').html();

There are plenty of places to read more about jQuery selectors, DOM traversing and DOM manipulation, some of the most vital parts of jQuery and basic building blocks for exciting functionality! Check out jQuery for Designers, a great collection of simple, hands on tutorials and screencasts. Also look at the jQuery official documentation, which is a comprehensive reference.

Hope this helps!

Upvotes: 0

Sampson
Sampson

Reputation: 268374

If you want your result to have HTML in it (your span tags), use the .html() method. If not, use the .text() method as others have suggested:

$(".leftpanel_anchor").html(); //Returns everything, including <span> tags
$(".leftpanel_anchor").text(); //Returns only the text, minus any tags

Upvotes: 3

Dan F
Dan F

Reputation: 12052

What's wrong with $('.leftpanel_keywords').text()?

Upvotes: 0

Tim B&#252;the
Tim B&#252;the

Reputation: 63744

If I got the question right, there is a parent-child selector to do this:

$(".leftpanel_anchor > .leftpanel_keywords").text()

Upvotes: 1

karim79
karim79

Reputation: 342655

$('a.leftpanel_anchor .leftpanel_keywords').text();

That will only select the ones within an anchor.

If there is more than one such occurence of that, you should condider giving at least the anchor an ID.

Upvotes: 0

Ionuț G. Stan
Ionuț G. Stan

Reputation: 179139

$(".leftpanel_keywords").text();

Upvotes: 0

Jeff Fritz
Jeff Fritz

Reputation: 9861

Depending on the structure of your entire page.. there are several different ways you may want to format your selector:

$(".leftpanel_keywords").text()

will get you any element that has the class "leftpanel_keywords"

$("A.leftpanel_anchor .leftpanel_keywords")").text()

will get you the inner class "leftpanel_keywords" for any anchor that has a class "leftpanel_anchor"

Both of these will give you the text inside of the span.

Upvotes: 1

Related Questions