html - Jquery length property always returning 0 -


i have form elements added via ajax in table below.

<tr>     <td></td>     <td>         <input id="cde[0]" name="cde[0]" class="required" value="30-09-2015">         <input type="hidden" id="cddbe[0]" name="cddbe[0]" value="2015-09-30">     </td>     <td>         <input id="cqtye[0]" name="cqtye[0]" class="required number" value="30">     </td>     <td>         <input id="cremarke[0]" name="cremarke[0]" value="remarks 1">     </td> </tr> 

there may many rows of elements in table.

i trying find if element exists before getting value using jquery.length property below

var nor=$('#commite tr').size()-2; var c="cqtye["+nor+"]"; if($('#'+c).length){   console.log("element exists"); } 

but .length false. when check length elements outside table returning 1. on elements inside table 0.

this table inside div , bith div , table have unique ids.

can 1 guide me how check if element exists in case.

as using [] i.e. meta-characters in id, these needs escaped.

use

var c="cqtye\\["+nor+"\\]"; 

docs

to use of meta-characters ( such !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) literal part of name, must escaped with 2 backslashes: \\.

$(function() {    var nor = $('#commite tr').length;    var c="cqtye\\["+nor+"\\]";    if ($('#' + c).length) {      snippet.log("element exists");    }  })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <!-- provides `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->  <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>  <table>    <tr>      <td></td>      <td>        <input id="cde[0]" name="cde[0]" class="required" value="30-09-2015">        <input type="hidden" id="cddbe[0]" name="cddbe[0]" value="2015-09-30">      </td>      <td>        <input id="cqtye[0]" name="cqtye[0]" class="required number" value="30">      </td>      <td>        <input id="cremarke[0]" name="cremarke[0]" value="remarks 1">      </td>    </tr>  </table>


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -