0

I've tried to use regex to find all elements that have indexed id "modal[index]" like this

$("input[id=modal[\[0-9\+]]").attr("disabled", "false");

this error raised

Uncaught Error: Syntax error, unrecognized expression: input[id=modal[[0-9+]]

and tried using for loop but the same error occured

for(var j=1;j<11;j++)
    $("input[id=modal["+j+"]]").attr("disabled", "false");

how to find these elements ?

Mohammad
  • 739
  • 7
  • 22

4 Answers4

0

var regix = new RegExp([0-9+]); futher read about its functions like .test etc

Arsalan Afridi
  • 209
  • 3
  • 13
0

if your id looks something like modal[1], modal [2], then:

for(var j=1;j<11;j++) {
  $("input#modal["+j+"]").attr("disabled", "false");
}
0

You could use filter function of JQuery, below example should work for you. source

$('input')
  .filter(function() {
      return this.id.match('\[0-9\+]');
  }).attr("disabled", "false");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="modal[8]" value="this textbox will disabled"/>
<input type="text" id="modal[a]" value="this will not"/>

and if you want to achieve this with for loop consider below example.

i have made few changes to your for loop

for(var j=0;j<$("input[type='text']").length;j++)

$("input[id='modal["+j+"]']")

for(var j=0;j<$("input[type='text']").length;j++)
    $("input[id='modal["+j+"]']").attr("disabled", "false");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="modal[0]" value="this textbox will disabled"/>
<input type="text" id="modal[1]" value="this will not"/>
Community
  • 1
  • 1
Dhaval Pankhaniya
  • 1,996
  • 1
  • 15
  • 26
  • this solution works for all inputs that have indexed id .. for example if i have another input of id "mod[1]" this input will be disabled too . – Mohammad Nov 27 '18 at 20:22
  • your question `How to use regex in jquery attributes?` clearly mention that you were trying to find elements with regex so i provided you solution for that – Dhaval Pankhaniya Nov 28 '18 at 06:44
0

The solution that worked for me is to remove the disabled attribute from the input attributes using CSS selectors without using regex like so

$('input[id^=modal]').removeAttr("disabled");

thank you all for your answers

Mohammad
  • 739
  • 7
  • 22