zq29 Posted October 6, 2006 Share Posted October 6, 2006 I'm creating some form inputs dynamically with php and naming them in a box_n fashion (n being an incrementing int). Any ideas on how I would go about fetching the contents of all of these inputs with JavaScript? Do I somehow loop through all form objects and check a substring-ed name/id for a match, or is there a simpler way?Many thanks in advance. Link to comment https://forums.phpfreaks.com/topic/23191-finding-all-form-inputs-whose-nameid-begins-with/ Share on other sites More sharing options...
effigy Posted October 6, 2006 Share Posted October 6, 2006 It wouldn't let me post with the script tags. This will fetch the names.[code]<!-- javascript --> function find_boxes() { var boxes = new Array(); var inputs = document.getElementsByTagName('input'); for (var i = 0; i < inputs.length; i++) { var name = inputs[i].name; if (name.match(/^box_\d+$/)) { boxes.push(name); } } alert(boxes); }<!-- /javascript --><form name="test_form"> <input name="box_1"/> <input name="box_2"/> <input name="box_3"/> <input name="name"/> <input name="address"/> <input name="etc"/> <input name="box_4"/> <input name="box_5"/> <input name="box_6"/></form><input type="button" onclick="javascript:find_boxes()" value="Find the boxes"/>[/code] Link to comment https://forums.phpfreaks.com/topic/23191-finding-all-form-inputs-whose-nameid-begins-with/#findComment-105161 Share on other sites More sharing options...
fenway Posted October 6, 2006 Share Posted October 6, 2006 Yup, there really isn't another way other than actually checking each one; note that you shouldn't have the javascript: in a JS handler. Link to comment https://forums.phpfreaks.com/topic/23191-finding-all-form-inputs-whose-nameid-begins-with/#findComment-105184 Share on other sites More sharing options...
zq29 Posted October 7, 2006 Author Share Posted October 7, 2006 Thanks guys :) Link to comment https://forums.phpfreaks.com/topic/23191-finding-all-form-inputs-whose-nameid-begins-with/#findComment-105516 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.