How to get a set of switch identifiers contained in a div

I want to get a list of all the switches that are contained in a div using javascript or jquery.

eg,

<div id="container">
    <input type="radio" id="1">
    <input type="radio" id="2">
    <input type="radio" id="3">
     .... //total no of radio buttons are not known
</div>

I need an array containing the identifier of all the switches contained in the div.

arr[0]="1"
arr[1]="2"
arr[2]="3"
...
..
+3
source share
3 answers
var array = new Array();
$('#container input:radio').each(function (index) {
    array[index] = $(this).attr('id');
});
+6
source
var ids = $('#container :radio').map(function() { return this.id; }).get();
+6
source

This may work:

var arr= [];
$('#container :radio').each(function(){
 arr.push({ "the_id": this.id, "val":this.value })
});
//right now you have a json like array with ID and value and you can access it by `arr[0].the_id` and `arr[0].val`

You can only click on the identifier (no value, so no curly brace is required), and you can access elements such as arr[0]

0
source

Source: https://habr.com/ru/post/1755912/


All Articles