t4d_ t4d_ - 1 year ago 50
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
I did following:

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 Source

You can use this:

var enabledIds = [];
var i = 0;
      enabledIds[i] = '#'+this.id;      
      var value = $(enabledIds[i]).val();

Here is a working demo