javascript - How to select a list of elements by name? -
i want select elements start particular sequence. sequence work_xxxxxx. how achieve this?
i providing answers in both jquery , javascript best option use jquery because javascript have loop throught elements , match desired patteren time consuming process preference use jquery:
finds inputs attribute name starts 'work_' , puts text in them.
<input name="newsletter" /> <input name="work_man" /> <input name="work_boy" /> <script> $('input[name^="work_"]').val('work here!'); </script>
http://api.jquery.com/attribute-starts-with-selector/
http://api.jquery.com/category/selectors/
with javascript no builtin keyword this, can loop through elements , check match of desired pattern, way:
var idstart = "work_"; var componentcontainer = document.getelementbyid("containertable").getelementsbytagname("div"); (var = 0; < componentcontainer.length; i++) { var id = componentcontainer[i].id; if ((id.length >= idstart.length) && (id.substring(0, idstart.length - 1) == idstart))// can use regular expression here match desired pattern { //do something... } }
Comments
Post a Comment