0

I am trying to bring the variable name inside the selectors, but its not working, here is the code, from where I have taken the example

How to use javascript variables in jquery selectors

and here is my code

$(document).ready(function() {
    //var detail = sample.one;
    var detail = sample;

    $('#' + detail).click(function() {

    });
});
Community
  • 1
  • 1
Aryan
  • 123
  • 2
  • 9

4 Answers4

0

you need to put the value between " because you want to store a string in your variable.

var detail = "sample"
Iván Rodríguez Torres
  • 3,963
  • 3
  • 29
  • 43
0

Try this:

$(document).ready(function() {

    var detail = 'sample';

    $('#' + detail).click(function() {

    });
});
Harshad Hirapara
  • 462
  • 3
  • 12
0
$(document).ready(function() {
    //var detail = sample.one;
    var detail = 'sample';

    $('#' + detail).click(function() {

    });
});

<input type="button" id="sample" />

This is working.

sample is a string so use "

Avanish Kumar
  • 2,208
  • 3
  • 25
  • 33
0

create a variable with string value for example

var detail = 'sample';

Mowgli
  • 129
  • 1
  • 3
  • 9