javascript - Checking the names of elements from an array of objects selected by jquery -
i'm beginner in js , jquery library. i'd array of input fields particular name, , validate input. each of input fields have name ns[0], ns[1] etc. total number of fields have determined code, since fields generated javascript.
i know can have jquery address individual object this:
$("input[name=ns\\[0\\]]").val()
<input type="text" name="ns[0]">
.
however, how can array of these similiar elements, ns[0] ns[x] x has determined based on how many fields have been generated? have other fields different name patterns sharing same css class, using class not option. these boxes in particular div area, in same area other input fields, choosing input boxes of same area selects them well.
in other words, how use jquery check name of each input field, after getting entire array of input fields, check each individual name?
since have input fields of various names in area determined table id cntr1, select them $('#cntr1 input')
. can select individual fields using $("input[name=]")
. however, want do, select under $('#cntr1 input')
, , run loop on names, checking whether names match predetermined criteria. how can that?
the html code:
<table class="table" id="cnservers"> <thead> <tr> <th>type</th> <th>preference</th> <th>value</th> <th>name</th> </tr> </thead> <tr id="cntr0"> <td>cname</td><td><input type="text" name="cn_pref[0]" value=""></td><td> <input type="text" name="cn_val[0]" value=""></td><td> <input type="text" name="cn_name[0]" value=""> <a class="btn btn-danger" onclick="delfield(this.id);" id="cn_d0" > <span class="btn-label">delete </span> </a> <a class="btn btn-primary" onclick="addfield('cnservers','cn',10);" id="cn_a0" > <span class="btn-label">add </span> </td></tr> </table> [1]: http://i.stack.imgur.com/bm0jq.jpg
i must missing something. there reason can't use http://api.jquery.com/attribute-starts-with-selector/?
$('#cntr1').find('input[name^="ns"]')
regarding,
however, want do, select under $('#cntr1 input'), , run loop on names, checking whether names match predetermined criteria. how can that?
$("#cntr1 input").each(function(index, elem) { var $elem = $(elem), name = $elem.attr('name'); var namematchescondition = true; // replace condition if (namematchescondition) { // something! } });
edit 1:
well, id
still attribute of html element. $('[id^="cntr1"]')
... value of id
attribute of element doesn't contain #
. it's part of css/jquery selector. when using attribute style selectors, don't need it. though can't comment on performance of this.
ideally, want attach second class, js-cntr
elements created id
starting cntr
. though different name pattern elements may have 1 class, class styling. there no stopping attaching custom classes purely selection via js. accepted thing , why class name starts js-
, denote purely use via js selection.
Comments
Post a Comment