Ossama
Ossama

Reputation: 2433

Introduce variable within another in Javascript

How can I change the following variable so that I can introduce another variable within it.

the code immediately below is working:

$("tbody").on("change", "input", function (ev) 
{
    console.log(this.name, this.value,  ev.target.className);                           
    var checked = $('.capex_opex_1:checked')
    var in_checked =  $('input.capex_opex_1')                               
    if (checked.length > 1) {
    in_checked.not(this).prop('checked', false).checkboxradio('refresh');}
});

However when I introduce a variable in either var checked or var in_checked in the code below, I get the error at the bottom

var checked = "$('." + ev.target.className + ":checked')"
var in_checked =  "$('input."  +  ev.target.className +  "')"                               
if (checked.length > 1) {
in_checked.not(this).prop('checked', false).checkboxradio('refresh');}

error:

RPC:654 Uncaught TypeError: in_checked.not is not a function at HTMLInputElement. (RPC:654) at HTMLTableSectionElement.dispatch (jquery-2.2.4.js:4737) at HTMLTableSectionElement.elemData.handle (jquery-2.2.4.js:4549) at Object.trigger (jquery-2.2.4.js:7807) at HTMLInputElement. (jquery-2.2.4.js:7875) at Function.each (jquery-2.2.4.js:365) at jQuery.fn.init.each (jquery-2.2.4.js:137) at jQuery.fn.init.trigger (jquery-2.2.4.js:7874) at HTMLInputElement. (jquery.mobile-1.4.5.min.js:5) at Function.each (jquery-2.2.4.js:365)

Upvotes: 1

Views: 79

Answers (1)

Racil Hilan
Racil Hilan

Reputation: 25351

Don't surround it with double quotes otherwise you're effectively converting to a string and the jQuery selector will not work. Open and close the single quotes appropriately.

Try this:

var checked = $('.' + ev.target.className + ':checked');
var in_checked = $('input.' + ev.target.className);

Upvotes: 3

Related Questions