1

I have this input field

<input name="Ops[2][Duration]" type="text" value="1" row-number="2">

Which I'm trying to select by name but it seems that

$('input[name=Ops[2][Duration]]');

Gets confused with all the brackets. How can I sort this out ? I tried

$('input[name=Ops\[2\]\[Duration\]]');

But that didn't work either, I still get:

Error: Syntax error, unrecognized expression: input[name=Ops[2][Duration]]

Connor Bishop
  • 821
  • 10
  • 19
  • You have missed to wrap the attribute value with double quotes,it as to be like this $('input[name="Ops[2][Duration]"]'); – Lokesh_Ram Jul 25 '16 at 10:58

3 Answers3

2

In a jQuery selector the escape character is two backslashes: \\:

$('input[name=Ops\\[2\\]\\[Duration\\]]');

Alternatively you could put the attribute value in quotes:

$('input[name="Ops[2][Duration]"]');
Rory McCrossan
  • 319,460
  • 37
  • 290
  • 318
2

You can wrap the attribute value in quotes:

$('input[name="Ops[2][Duration]"]');
Milind Anantwar
  • 79,642
  • 23
  • 92
  • 120
1

Use the name attribute value in paired quotes

$('input[name="Ops[2][Duration]"]');

or

$("input[name='Ops[2][Duration]']");

$(function(){
    $('input[name="Ops[2][Duration]"]').on('blur', function(){
        console.log($(this).val());
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="Ops[2][Duration]" type="text" value="1" row-number="2">
Shubham Khatri
  • 246,420
  • 52
  • 367
  • 373