views:

57

answers:

1

I am wanting to take the text value of a p tag when I press enter

$.keynav.enterDown = function () {
  var accom = $('#suggestions p.keynavon').html();
  alert(accom);

}

This is working great but the results contain <span> and </span> tags, is there anyway I can strip those tags from the string?

+1  A: 

If you just want the text, which sounds like what you're after, use .text() instead of .html(), like this:

$.keynav.enterDown = function () {
  var accom = $('#suggestions p.keynavon').text();
  alert(accom);
}

If you actually need to strip the <span> tags specifically, clone the content (as to not affect the original) and replace them with their inner content via .replaceWith(), like this:

$.keynav.enterDown = function () {
  var accom = $('#suggestions p.keynavon').clone()
                .find('span').replaceWith(function() { return this.innerHTML; })
                .end().html();
  alert(accom);
}
Nick Craver
Cheers nick, i totally forgot about .text!Would you be able to help me with something else?is there anyway to take the text and then remove the characters(rest of text) after a - symbol and then remove the - itself so eg."some - text" would become"some"?
AJFMEDIA
@AJFMEDIA - I'm not sure *exactly* what you're after, but yes you can using `.indexOf()` to get the position of what you're after the slice or substring around that, I'd ask a question with an example of what you're after, input and desired result :)
Nick Craver