0
<a href="/JqueryTest/Delete/23" class="Delete-item">Delete</a>

if i use this JQuery code :

$('.Delete-item[href="/JqueryTest/Delete/23"]')

It work fine , but if i replace Value with my variable :

var MyVar= "/JqueryTest/Delete/23";
$('.Delete-item[href=MyVar]')

I have on error syntaxe, any help !

5 Answers 5

3

you have to concatenate the string in this way..

var MyVar= "/JqueryTest/Delete/23";
$('.Delete-item[href="' + MyVar + '"]');
Sign up to request clarification or add additional context in comments.

1 Comment

so you can accept the answer and upVote it too..if it helps you.. :)
2

It should be

var MyVar= "/JqueryTest/Delete/23";
$('.Delete-item[href="' + MyVar + '"]');

Concatenate the variable into the string

2 Comments

is " necessary here on "' + MyVar + '" ?
Yup, attributes need to be quoted
0

$('.Delete-item[href=' + MyVar + ']')

1 Comment

You forgot the " around the attribute
0

myVar in [href=myVar] is a part of a string not a variable. So you need to say
$('.Delete-item[href="' + MyVar + '"]')

Comments

0

try this

   var MyVar= "/JqueryTest/Delete/23";
    $('.Delete-item[href="'+MyVar+'"]')

1 Comment

You forgot the " around the attribute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.