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>
Related
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:
How can I count the number of elements with same class?
(6 answers)
Closed 5 years ago.
I've got the following html :
<div class="same"></div>
<div class="same"></div>
And I want to print the number of div with the id "same" like this
<p><script>document.write(getElementbyId("#same").sum);</script></p>
And it would print : 2...
It actually doesn't work, I know I don't use the good Javascript function.
You'd use .length and .getElementsByClassName() like this:
<div class="same"></div>
<div class="same"></div>
<p><script>document.write(document.getElementsByClassName("same").length);</script></p>
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();
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");
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];