Skip to content
Advertisement

Tag: dom

Adding HTML elements with JavaScript

So, if I have HTML like this: How can I use JavaScript to add an HTML element where that blank line is? Answer As you didn’t mention any use of javascript libraries (like jquery, dojo), here’s something Pure javascript. or

Select Element By CSS style (all with given style)

Is there a way to select all elements that have a given style using JavaScript? Eg, I want all absolutely positioned elements on a page. I would assume it is easier to find elements by style where the style is explicitly declared: the style is non-inherited (such as positioning) the style is not the default (as would be position:static). Am

Detect changes in the DOM

I want to execute a function when some div or input are added to the html. Is this possible? For example, a text input is added, then the function should be called. Answer 2015 update, new MutationObserver is supported by modern browsers: Chrome 18+, Firefox 14+, IE 11+, Safari 6+ If you need to support older ones, you may try

JavaScript – Get all but last item in array

This is my code: I want linkElementLink.href to grab everything but the last item in the array. Right now it is broken, and the item before it gets the second-to-last item. Answer Use pathname in preference to href to retrieve only the path part of the link. Otherwise you’ll get unexpected results if there is a ?query or #fragment suffix,

Accessing the content of other tabs in a browser

I am using Mozilla Firefox and I am trying to figure out a way to access the content of other tabs in the same window using JavaScript and the DOM (I am open to other techniques if exist). E.g., I want to run JavaScript code in tab1 which can find the title of some other tab. Basically I need this

Best way to add DOM elements with jQuery

So I’ve seen three ways to add html/DOM elements to a page. I’m curious what the pros and cons are for each of them. 1 – Traditional JavaScript I believe the straight JS way to do it is by constructing each element, setting attributes, and then appending them. Example: 2 – Appending a string of html via jQuery I’ve noticed

How to set DOM element as first child?

I have an element E and I’m appending some elements to it. All of a sudden, I find out that the next element to append should be the first child of E. What’s the trick, how to do it? Method unshift doesn’t work because E is an object, not array. Long way would be to iterate through E’s children and

How to get a DOM Element from a jQuery selector?

I’m having an impossibly hard time finding out to get the actual DOMElement from a jQuery selector. Sample Code: and in another piece of code I’m trying to determine the checked value of the checkbox. And please, I do not want to do: That gets me around the checkbox, but other times I’ve needed the real DOMElement. Answer You can

How do I set/unset a cookie with jQuery?

How do I set and unset a cookie using jQuery, for example create a cookie named test and set the value to 1? Answer Update April 2019 jQuery isn’t needed for cookie reading/manipulation, so don’t use the original answer below. Go to https://github.com/js-cookie/js-cookie instead, and use the library there that doesn’t depend on jQuery. Basic examples: See the docs on

Advertisement