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
  • 133
  • 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
  • 4,293
  • 3
  • 31
  • 47
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 "

A.K.
  • 2,284
  • 3
  • 26
  • 35
0

create a variable with string value for example

var detail = 'sample';

Mowgli
  • 129
  • 1
  • 3
  • 9