user3342042
user3342042

Reputation: 241

Jquery - check href in link expression issue?

I stuck on the expression that check if address contains variable. Jquery returns error: unrecognized expression: .ui-body h3 a[href*=/65_] . Can you help me with this please. Thank you very much. PS: I need that slash in href expression with number and underscore because of duplicity in links. Slash in expression is problem !

Jquery:

num_id = 65
$(".ui-body h2 a[href*=\/" + num_id + "]_").first().parent().parent().parent().append($(this));

HTML: ..content

<div class="col-bottom-1">
  <div class="ui-body">
   <h2>
     <a href="/65_news.html">Text</a>
     <a href="/65_news.html">&gt;&gt;</a>
   </h2>
  </div>
  <!-- this is place where I display div after Jquery function -->
</div>

Upvotes: 1

Views: 56

Answers (2)

Irvin Dominin
Irvin Dominin

Reputation: 30993

You must escape your / to use it as selector and your _ must be inside the href attribute selector.

Ref:

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \. For example, an element with id="foo.bar", can use the selector $("#foo\.bar"). The W3C CSS specification contains the complete set of rules regarding valid CSS selectors. Also useful is the blog entry by Mathias Bynens on CSS character escape sequences for identifiers.

Like:

num_id = 65
$(".ui-body h2 a[href*=\\/" + num_id + "_]").first().parent().parent().parent().append($(this));

Demo: http://jsfiddle.net/IrvinDominin/GUDYN/

Upvotes: 2

adeneo
adeneo

Reputation: 318222

I'm guessing it's supposed to be

 $(".ui-body h2 a[href*='/" + num_id + "_']")

FIDDLE

don't escape inside the brackets, and make sure all characters (the underscore) are actually inside the brackets

Upvotes: 1

Related Questions