0

I have this string that can represent two things either some text or an anchor tag with text. I wrote something to always return the text like follows:

$(text).is('a') ? $(text).text() : text;

My logic is that if text is an anchor tag ? return the content of the anchor tag : if it's not then it's already just text so return that.

My question is that why does the following expression return true:

$('a').is('a');

Is is checking for the letter or anchor element? How can I check if my text is an anchor tag.

I would prefer not using any regular expressions

Edit:

I have a variable x that can have these values:

x = 'some text' or x = '<a>some text</a>'

How can I always extract the text from my variable x.

Dave Gray
  • 9
  • 2

4 Answers4

0

'a' is a valid query selector. It will select all the anchor elements in your document. So $('a').is('a') is saying "find anchor tags in the document and tell me if they are anchor tags" -- this will always be true unless there are no anchor tags in your document.

IceMetalPunk
  • 5,476
  • 3
  • 19
  • 26
-1

I have this string that can represent two things either some text or an anchor tag with text. I wrote something to always return the text like follows:

$(text).is('a') ? $(text).text() : text;

My logic is that if text is an anchor tag ? return the content of the anchor tag : if it's not then it's already just text so return that.

If possible, I would avoid having text be vague like that.

You certainly can't just dump the text into $() and assume all will be well. For instance, $("Some text here") searches the DOM for elements with the tag here inside elements with the tag text inside elements with the tag Some.

You've said you want to differentiate between

text = "Some text here"

and

text = "<a>Some text here</a>"

I'd just look at the string. Inspired partially by jQuery's check for whether what you pass it is a selector or HTML (here), how about:

text = text.trim();
if (text.substring(0, 2) === "<a" && text.substr(-1) === ">") {
    text = $(text).text();
}

or similar?

But again, I'd avoid putting myself in this position in the first place if you have any choice.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
-1

Just set the string you have to an element's html. Grab the text of the element and this way you do not worry about if it is an anchor or plain text.

function getText (content) {
  var div = document.createElement("div")
  div.innerHTML = content
  return div.textContent
}

console.log('some text', getText('some text'))
console.log('<a>some text</a>', getText('<a>some text</a>'))

If you want to use jQuery

function getText (content) {
  return $("<div></div>", {html: content}).text()
}

console.log('some text', getText('some text'))
console.log('<a>some text</a>', getText('<a>some text</a>'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • @DaveGray It's just wrapping the contents in a new div element, that way it ends up being an element regardless of whether it's a text string or an anchor tag. Then you can just use `.text()` on it regardless of its original form because it will always be an element at that point. – IceMetalPunk Oct 30 '19 at 16:45
-1

I have been unable to replicate the issue as you explained it. Please see: https://api.jquery.com/is/

Here is my testing:

$(function() {
  var tableData = [
    "Text 1",
    "Text 2",
    "<a>Text 3</a>",
    "Text 4"
  ];

  function isLink(el) {
    return $(el).is("a");
  }

  var x = [];
  $.each(tableData, function(i, s) {
    x.push(isLink(s) ? $(s).text().trim() : s);
  });
  $(".results").html(x.join(", "));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="results"></div>

You can pass Text, Element, or even a jQuery Object to the function.

Twisty
  • 30,304
  • 2
  • 26
  • 45