views:

193

answers:

7

I must use jQuery for first time....

<a class="Tag Resource" href="http://localhost/" 
resource="http://localres/" property="prop">test</a>

I've tried to extract the text using var = $('a').find('Tag Resource').text(); and var = $('a').find('Tag Resource').html(); but it doesn't work. I need "test" as plain text.

Can someone tell me how to do this?

Thanks in advance

+6  A: 

I think you're looking for:

var t = $("a.Tag.Resource").text();

meaning a tags that have both the Tag and Resource classes. The find() method is for searching subtrees of elements.

cletus
This doesn't get the text for the specific tag Sirakov is looking for. The one with a class of "Tag Reference".
Chris Missal
var t = $("a").text(); - this give me not the text
cupakob
+1  A: 

I think the problem is the syntax of your find expression.

Update: In fact, you don't want find at all, you want filter. Find will only select descendants of the a elements, rather than the elements themselves.

I've tested the example line below.

From the example here, it looks like you want

var text = $('a').filter('.Tag.Resource').text();
Mike Houston
A: 
var text = "";
$("a").each(function(){
  text += $(this).html() + " " + $(this).attr("resource");
});
alert(text);
andres descalzo
+5  A: 

Here you go (live demo):

$(document).ready(
  function (){ 
    alert(  $('a.Tag.Resource').html()  );  
});

Your issue is either that you wanted one class but used a space so it became two; or that when referring to classes with a jquery selector, you need to prefix them with a period.

In any case, the above code will help. If you really just wanted one class, change it to $('a.Tag-Resource')...

Michael Haren
Haven't seen jsbin before, nice tool! :)
Mike Houston
A: 

I don't think you can have class names with spaces in. You've added 2 classes "Tag" and"Resource" to the a tag and your find selectory won't find that.

John Burton
A: 

Remember, class names can be repeated on a page, and spaces indicate two classes applied to an element. You are not guaranteed that a single element will have that class, so .text() may return the combined text of all matched elements.

$(".Tag.Resource").text();
Peter J
A: 

Well, you don't have to use JQuery...

var text, links = document.links;
for (var i = 0; i < links.length; i++) {
   if (links[i].className == 'Tag Resource') {
      text = links[i].innerText;
      break;
   }
}
alert(text);
Guffa