SamDZ
SamDZ

Reputation: 79

use jquery attribute with variable in value place

<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 !

Upvotes: 0

Views: 2727

Answers (5)

Gowri
Gowri

Reputation: 16835

try this

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

Upvotes: 0

sv_in
sv_in

Reputation: 14039

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

Upvotes: 0

Vivek
Vivek

Reputation: 11028

you have to concatenate the string in this way..

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

Upvotes: 3

Jeaf Gilbert
Jeaf Gilbert

Reputation: 11981

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

Upvotes: 0

JohnP
JohnP

Reputation: 50019

It should be

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

Concatenate the variable into the string

Upvotes: 2

Related Questions