Skip to content

Tag: dom

Difference between document.URL and location.href

I know that document.URL can not be set, while location.href can. But the Document indicates: URL is a replacement for the DOM Level 0 location.href property. So when would we use document.URL? Answer You can get the document.URL, but you can not set it. You can both get and set the location.href. In some web…

using javascript’s .insertBefore to insert item as last child

I so miss jQuery. I’m working on a project where I need to get my hands dirty with good ‘ol plain Javascript again. I have this scenario: Via javascript, I want to be able to insert a new node before or after any of those children. While javascript has an insertBefore, there is no insertAfter. Ins…

What is the DOM and BOM in JavaScript?

What is the DOM and BOM in JavaScript? If someone could explain these in layman terms it would be great! I like to get a deeper understanding of these. Answer The BOM (Browser Object Model) consists of the objects navigator, history, screen, location and document which are children of window. In the document …

Remove all child elements of a DOM node in JavaScript

How would I go about removing all of the child elements of a DOM node in JavaScript? Say I have the following (ugly) HTML: And I grab the node I want like so: How could I remove the children of foo so that just <p id=”foo”></p> is left? Could I just do: or should I be using some combin…

Append Style to DOM not Replacing Existing

How can I append style element to DOM without eliminating existing style on the item (eg color, text-align, etc)? The event calls the function, but the problem is ‘Style’ gets completely replaced with the single item instead. I have simple code triggered on the event: Answer Which browser are you …

jQuery: how to change tag name?

jQuery: how to change tag name? For example: I need Yes, I can Create DOM element <div> Copy tr content to div Remove tr from dom But can I make it directly? PS: results in DOMException. Answer You can replace any HTML markup by using jQuery’s .replaceWith() method. example: http://jsfiddle.net/JH…