This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get the attributes of a HTML element using JQuery?
I have a piece of JS and I need to read property 'value' from a div.
I do this:
$('#calendar_event_path')
I get this as and answer:
[<div id="calendar_event_path" value="company_events_index"></div>]
How can I get "company_events_index" value from the div?
use data-value attribute in your html
<div id="calendar_event_path" data-value="company_events_index"></div>
the in jQuery fetch its value using data()
var sumthng = $('#calendar_event_path').data("value");
Related
This question already has answers here:
Getting the parent div of element
(7 answers)
Add CSS attributes to element with JavaScript
(5 answers)
Set CSS property in JavaScript?
(11 answers)
Closed 3 months ago.
I'm trying to translate this jQuery into JavaScript, to save a bit on load time. Here's the line of jQuery:
$('div.annotated p > span.citation').parent().css('display', 'inline')
And here's what I have so far in JavaScript:
document.querySelectorAll('div.annotated p > span.citation').forEach((el) => {})
I'm stuck on how to find the parent of an element, and also apparently how to set the CSS for that element. Should I stick with jQuery, or can JavaScript also do this, and is it much harder?
This question already has answers here:
Create <div> and append <div> dynamically
(9 answers)
Closed 2 years ago.
I used a query selector to get the div i want to append. But the created div doesn't want to append to anything other than the body. Is there a way i can make it specifically append under the div?
Look at this: https://www.w3schools.com/jsref/met_node_appendchild.asp
What you need to do is
parent.appendChild(thatDiv);
This question already has answers here:
Set attribute without value
(7 answers)
Closed 4 years ago.
I want to add an attribute on a div, something like this:
<div class='media' data-type-sticky></div>
Using jQuery, so i've tried this:
jQuery('.media').attr(data-type-sticky);
jQuery('.media').attr('data-type-sticky');
Just pass empty string to attr value.
$('.media').attr('data-type-sticky','');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='media'>Hello</div>
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:
How to get the entire document HTML as a string?
(16 answers)
Closed 5 years ago.
I know there's a way to do this, but I cannot recall how.
How do I go about grabbing the HTML element (top-most element in the DOM), using plain JavaScript?
Use document.documentElement.
See the docs: https://developer.mozilla.org/en-US/docs/Web/API/Document/documentElement
var html = document.getElementsByTagName('html')[0];