0

I have to get value from textbox its name as an array.but i get response as undefined.

<input id="text[0][0]" type="text" value="" class="form-control label text-center" placeholder=" 1" style="">
<button>Text Box Value</button> 

I tried jquery code

var button = $("button")
button.on("click", function(){
   var cell= $("#text[0][0]").val()
   alert(cell)
}))
samjad ps
  • 73
  • 1
  • 8
  • Try `document.getElementById('text[0][0]').value` – User863 Aug 31 '19 at 08:55
  • Possible duplicate of [jQuery - help needed on ID selector when id is an array type notation](https://stackoverflow.com/questions/4254407/jquery-help-needed-on-id-selector-when-id-is-an-array-type-notation) – Rohit Mittal Aug 31 '19 at 09:00
  • 1
    Possible duplicate of [Find DOM element by ID when ID contains square brackets?](https://stackoverflow.com/questions/1239095/find-dom-element-by-id-when-id-contains-square-brackets) – Nidhin Joseph Aug 31 '19 at 09:35

2 Answers2

1

You can use \\ to escape square bracket in jquery selector

https://learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css-notation/

var button = $("button")
button.on("click", function() {
  var cell = $("#text\\[0\\]\\[0\\]").val()
  alert(cell)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input id="text[0][0]" type="text" value="1">
<button>Text Box Value</button>

Note document.getElementById('text[0][0]') works without escaping square brackets

User863
  • 19,346
  • 2
  • 17
  • 41
0

You can do it as follow using Javascript.

$(document).ready(function(){
     var button = $("button");
     button.on("click", function(){
        var cell= document.getElementById('text[0][0]').value
        alert(cell)
     })
   });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="text[0][0]" type="text" value="" class="form-control label text-center" placeholder=" 1" style="">
<button>Text Box Value</button>
Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43