This question already has answers here:
jquery get only all html elements with ids
(3 answers)
Closed 8 years ago.
Simple. I want to traverse the DOM and find all elements that have an id attribute. Can someone help me with a js and/or jquery script
You can query all the elements with an id attribute by using the following jQuery selector:
$("[id]")
You can also just use plain JavaScript using querySelectorAll:
document.querySelectorAll("[id]")
If you want to find non-empty id attributes, use this selector instead:
'[id]:not([id]="")'
Simple attribute selector
$('[id]');
Reference : Has Attribute Selector [name]
Related
This question already has answers here:
document.querySelector multiple data-attributes in one element
(3 answers)
Closed 2 years ago.
I have a very specific situation here that I can't find anywhere else.
I need to point to a <link> element that contains the rel="me" attribute AND an href="https://www.example.com/profile/12345" attribute that CONTAINS 12345.
<link rel="me" href="https://www.example.com/profile/12345"/>
I tried with document.querySelectorAll() but I don't know how to format it to make the AND operator work.
if (document.querySelectorAll('[rel="me"], a[href*=123456]')[0])
Any ideas on how to do this?
In a CSS selector "AND" is just not using a combinator at all.
If you have a type (or universal) selector, it has to go first.
The type selector to select <link> elements is link not a.
link[rel="me"][href*="12345"]
If you want the first match, use querySelector() instead of querySelectorAll()[0]
const el = document.querySelector('link[rel="me"][href*="12345"]');
console.log(el);
<link rel="me" href="https://www.example.com/profile/12345"/>
This question already has answers here:
Can jQuery get all CSS styles associated with an element?
(5 answers)
Closed 5 years ago.
I am using jQuery to edit html elements using .each on all off a class. If I try to log any of the attributes then I will just get "" even though I have set the attributes to something in my css file.
Why is this happening? Can the javascript file not access the css attributes?
As I'm unclear whether you mean CSS attributes or DOM element attributes, here's an explanation for both:
To loop through some elements before reading their (in this case value) attributes, you can do this like:
$('.element').each(function(){
console.log( $(this).attr('value') );
});
To loop through some elements before reading their CSS properties, you can do this like:
$('p').each(function(){
console.log( $(this).css('width') );
});
Take a look at the console to see the three elements width being output: https://jsfiddle.net/nrfxn9nb/
This question already has answers here:
jQuery : select all element with custom attribute [duplicate]
(2 answers)
Closed 6 years ago.
Is there a way to grab a whole element by one of its data attributes via javascript or jQuery? Say i have a link somewhere on a website like this
Google
Since the link above doesn't have any id or class i cannot grab it by that. I need to get it by its data-source attribute. So say i could somehow with jQuery get the value of a attribute with the name of data-source. How would i do that ?
$('[data-source="google"]')
This will select all elements which data-source attribute equals to google.
See Attribute Equals Selector [name=”value”].
This question already has answers here:
JQuery class selector vs id selector
(5 answers)
Closed 8 years ago.
HTML:
JQuery:
('.nameClass').click(myFunction);
or
('#txtName').click(myFunction);
In Jquery Which is best perform when we have 100's of control to apply this logic. And Why better perform ?
Using id selector will be fast always as it returns single element but class selector will return one or more elements.
NOTE - You must ensure that id should be unique through out the DOM.
This question already has answers here:
How can I get the elements without a particular attribute by jQuery
(7 answers)
Closed 9 years ago.
I'm trying to use jquery to grab all select elements which do not have the "multiple" attribute set. I tried with the :not() but I can't get it quite right.
$(document).ready(function() {
$('select:not(multiple)').select2();
});
You forgot the attribute selector. What you're looking for there is "a <select> that is not a <multiple>"
$("select:not([multiple])").select2();
$('select:not([multiple])').select2();
or
$('select').not('[multiple]').select2();