@manhng

Welcome to my blog!

Find IDs

June 24, 2019 09:19

Get all Ids of all elements in document (edit)

https://stackoverflow.com/questions/7115022/how-do-i-enumerate-all-of-the-html-ids-in-a-document-with-javascript

var allElements = document.getElementsByTagName("*");

var allIds = [];

for (var i = 0, n = allElements.length; i < n; ++i) {
var el = allElements[i];
if (el.id) { allIds.push(el.id); }
}

var s=''; for(var i=0;i<allIds.length;i++) { s = s + allIds[i] + ';'; } alert(s);

console.log(document.querySelectorAll('*[id]:not([id=""])'));

var IDs = [];
$("#mydiv").find("span").each(function(){ IDs.push(this.id); });

Class name

<!-- HTML -->
<a class="test" name="Name 1"></a>
<a class="test" name="Name 2"></a>
<a class="test" name="Name 3"></a>

// Javascript
var elements = document.getElementsByClassName("test");
var names = '';
for(var i=0; i<elements.length; i++) {
    names += elements[i].name;
}
alert(names);

Toggle class

https://www.techrepublic.com/blog/web-designer/jquery-how-to-get-objects-by-id-class-tag-and-attribute/

$('#article-switch').click(function () {
    $('article').toggleClass('newClass');
});

Properties of object

https://stackoverflow.com/questions/208016/how-to-list-the-properties-of-a-javascript-object

var keys = Object.keys(myObject);
var getKeys = function(obj){
   var keys = [];
   for(var key in obj){
      keys.push(key);
   }
   return keys;
}

Pure Javascript

https://www.digitalocean.com/community/tutorials/how-to-access-elements-in-the-dom

https://flaviocopes.com/how-to-list-object-methods-javascript/

http://xahlee.info/js/js_get_elements.html

Element's ID that Start with

https://salesforce.stackexchange.com/questions/122836/find-all-elements-id-starting-with-or-ending-with-using-jquery-in-visualforce

$(document).ready(function(){
$('input[id*=Name]' ).each(function(el){
el.val('Foo'); // do something with the input here.
});
});

Categories

Recent posts