I have 100 input type text, which are placed in a table. They have no id attribute. But they have unique name attribute. I want to find the names of all the textboxes and save in an array so that i can use them further. Any help would be appreciated.
Asked
Active
Viewed 1,038 times
0
-
Have you tried anything? – Rajesh Apr 27 '17 at 06:07
-
1I'm voting to close this question as off-topic because question does not show any sign of effort – Rajesh Apr 27 '17 at 06:13
-
[How to get name of input](http://stackoverflow.com/questions/3513809/get-attribute-name-value-of-input) + [Looping through all inputs](http://stackoverflow.com/questions/4758259/looping-through-input-fields-for-validation-using-jquery-each) should do the trick – Rajesh Apr 27 '17 at 06:18
3 Answers
3
Try with Array#each
and attr('name')
using jquery and for text
match only using input:text
var a=[];
$('input:text').each(function(){
a.push($(this).attr('name'))
})
console.log(a)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="one" ><input type="text" name="two" ><input type="text" name="three" ><input type="text" name="four" ><input type="text" name="five" ><input type="text" name="six" >

prasanth
- 22,145
- 4
- 29
- 53
-
-
1OP should better use `$(":text")` if there're more than just those 100 inputs in table – Nitro.de Apr 27 '17 at 06:13
1
Loop over inputs
and use .each()
function to iterate over each item, then get the names via .prop()
function.
var names = [];
$('input').each((k,v) => names.push($(v).prop('name')));
console.log(names);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="a">
<input name="b">
<input name="c">
<input name="d">

Suren Srapyan
- 66,568
- 14
- 114
- 112
-
-
1@Rajesh This is a difficult question. It depends on the content of the question. – Suren Srapyan Apr 27 '17 at 06:15
-
May be its difficult, but still you **have** to share something. Even Google search links would help. But this is just a wish and not a problem. Also if you break your problem, it becomes simple – Rajesh Apr 27 '17 at 06:16
1
use this code to get all inputs
html file :
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<table>
<tr>
<td><input name="input1" /></td>
<td>test</td>
</tr>
<tr>
<td><input name="input2" /></td>
<td>test</td>
</tr>
<tr>
<td><input name="input3" /></td>
<td>test</td>
</tr>
</table>
<button id="button">
submit
</button>
<div id="result">
</div>
JS :
$(document).ready(function(){
$('#button').click(function(){
var data = []
$('input').each(function() {
data.push({
"name": $(this).attr('name'),
"value": $(this).val()
})
});
console.log(data)
$('#result').text(JSON.stringify(data))
});
});

Amir Mohsen
- 853
- 2
- 9
- 23