How to get all of the IDs with jQuery?
//but i cannot really get the id and assign it to an array that is not with in the scope?(or can I)
Yes, you can!
var IDs = [];
$("#mydiv").find("span").each(function(){ IDs.push(this.id); });
This is the beauty of closures.
Note that while you were on the right track, sighohwell and cletus both point out more reliable and concise ways of accomplishing this, taking advantage of attribute filters (to limit matched elements to those with IDs) and jQuery's built-in map()
function:
var IDs = $("#mydiv span[id]") // find spans with ID attribute
.map(function() { return this.id; }) // convert to set of IDs
.get(); // convert to instance of Array (optional)
jQuery - find all IDs in a class
Use .each()
var idArray = [];
$('.red').each(function () {
idArray.push(this.id);
});
Jquery, getting back list of ID strings of all elements with ID's that begin with a string?
Live Demo
var elements = [];
$("div[id^='tagRow_']").each(function(){
elements.push(this.id);
});
var stringOfElementIDs = elements.toString();
Get id's of all child elements with a particular class
Use map().
Also, correct your HTML markup like in the snippet(class=""
).
Finally, use .get() if you need a true JavaScript array (instead of a jQuery collection of strings — jQuery's collections [sets] usually contain DOM elements, but can contain anything).
var ids = $('#ulList .vehicle').map(function(){ return $(this).attr('id'); }).get();
console.log(ids);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><ul id="ulList"> <li id="car" class="vehicle"></li> <li id="bus" class="vehicle"></li> <li id="cat" class="animal"></li> <li id="dog" class="animal"></li> <li id="bike" class="vehicle"></li> <li id="monkey" class="animal"></li></ul>
How to get all table ids inside a div using jquery
Try,
$('#myDiv table').each(function(){
alert(this.id);
});
Working Demo
get ALL id's of children elements
Use jQuery's each
and push
the id
to the array:
var parentDiv = [];$("#parent-div > div").each((index, elem) => { parentDiv.push(elem.id);});
console.log(parentDiv);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script><div id="parent-div"> <div id="div-no-1"></div> <div id="div-no-2"></div> <div id="div-no-3"></div> <div id="div-no-4"></div></div>
How to get all the ids of elements inside a list using jquery
try:
$('#list>li').each(function(){ alert($(this).attr('id')); })
Related Topics
Replace All Spaces in a String with '+'
Ipad/iPhone Browser Crashing When Loading Images in JavaScript
Getting a Jquery Selector for an Element
How to Replace a Regex Substring Match in JavaScript
Pass JavaScript Variable to Flask Url_For
How to Create a Simple Http Proxy in Node.Js
Change Route Params Without Reloading in Angular 2
Using Await Outside of an Async Function
CSS Media Queries and Jquery Window .Width() Do Not Match
Tried to Load Angular More Than Once
Thickness of Lines Using Three.Linebasicmaterial
Lodash Remove Duplicates from Array
Aggregation Filter After $Lookup
Google Bar Chart Cannot Change Individual Bar Color
Chaining .Bind() Calls in JavaScript. Unexpected Result