t4d_ t4d_ - 4 months ago 9
jQuery Question

return enabled inputs ids and create variables from them in jquery

My goal is to create variables from enabled input ID's and create selector variables from them.

This is example of HTML:

<input type="number" class="numberInput" id="input1"/>
<input type="number" class="numberInput" id="input2" disabled/> <!-- Do not return this id -->
<input type="number" class="numberInput" id="input3"/>
<input type="number" class="numberInput" id="input4"/>
<input type="number" class="numberInput" id="input5" disabled/> <!-- Do not return this id -->


So the function should return only
#input1
,
#input3
and
#input4
.
I did following:

$(".numberInput:enabled").each(function(){
var enabledInputId = this.id;
});


Which does the trick, but I want to make variables from returned ID.

Something like:

var value1 = $("#" + returnedId); //index 0 -- should return #input1
var value2 = $("#" + returnedId); //index 1 -- should return #input3
var value3 = $("#" + returnedId); //index 2 -- should return #input4


NOTE: Disabled and enabled inputs will be changed on different events, so I want only make variables which are enabled at this moment.

Answer

You can use this:

var enabledIds = [];
var i = 0;
$(".numberInput:enabled").each(function(){
      enabledIds[i] = '#'+this.id;      
      console.log(enabledIds[i]);
      var value = $(enabledIds[i]).val();
      console.log(value);
      i++;
    }); 

Here is a working demo

Comments