Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error, unrecognized expression for href

Tags:

jquery

When I add below script and run. I am getting this:

Uncaught Error: Syntax error, unrecognized expression: ul li a[href=#!id1]

I am not sure which double quote causing the issue.

HTML

<ul>
 <li class="slist selected" id="id1"><a href="#!id10">Test1/a></li>
 <li class="slist" id="id2"><a href="#!id20">Test2</a></li>
 <li class="slist" id="id3"><a href="#!id30">Test3/a></li>
</ul>

JS

$(document).ready(function () {
    var id = "#!" + window.location.href.split("!")[1];
    if ($("ul li a[href=" + id + "]").length) {
        console.log("present");    
    } else {    
        console.log("absent")
    }
});
like image 212
TDG Avatar asked Jul 03 '15 02:07

TDG


3 Answers

You need to enclose special characters in quotes when using a attribute based selector.

if ($('ul li a[href="' + id + '"]').length) {

Your version of selector would result

if ($("ul li a[href=#!...]").length) {

The #! will throw unrecognized expression.


My version where the "" escape the characters

if ($('ul li a[href="#!..."]').length) {
like image 80
Shaunak D Avatar answered Nov 11 '22 10:11

Shaunak D


I tried the solution provided by

https://github.com/jquery/jquery/issues/2885

which worked for me. I search for [href=#] in js and replace with [href*=\\#]

a[href*=\\#]:not([href=\\#])
like image 26
Bikram Shrestha Avatar answered Nov 11 '22 10:11

Bikram Shrestha


you may add the below code in functions.php

function modify_jquery() {
if (!is_admin()) {
	wp_deregister_script('jquery');
	wp_register_script('jquery', 'https://code.jquery.com/jquery-1.11.3.min.js');
	wp_enqueue_script('jquery');
}
}
add_action('init', 'modify_jquery');
like image 4
Sebastian Diaz Avatar answered Nov 11 '22 09:11

Sebastian Diaz