Skip to content
Advertisement

Tag: dom

JavaScript moving element in the DOM

Let’s say I have three <div> elements on a page. How can I swap positions of the first and third <div>? jQuery is fine. Answer Trivial with jQuery If you want to do it repeatedly, you’ll need to use different selectors since the divs will retain their ids as they are moved around.

Is there a way to select sibling nodes?

For some performance reasons, I am trying to find a way to select only sibling nodes of the selected node. For example, If I selected inner1 node, is there a way for me to access its siblings, inner2-4 nodes? Answer Well… sure… just access the parent and then the children. or… using jQuery: Edit: Cletus as always is inspiring. I

“Submit is not a function” error in JavaScript

Can anyone tell me what is going wrong with this code? I tried to submit a form with JavaScript, but an error “.submit is not a function” shown. See below for more details of the code: I also tried this: Both show me the same error 🙁 Answer submit is not a function means that you named your submit button

How do I programmatically click on an element in JavaScript?

In IE, I can just call element.click() from JavaScript – how do I accomplish the same task in Firefox? Ideally I’d like to have some JavaScript that would work equally well cross-browser, but if necessary I’ll have different per-browser JavaScript for this. Answer The document.createEvent documentation says that “The createEvent method is deprecated. Use event constructors instead.” So you should

Get next / previous element using JavaScript?

How do I get the next element in HTML using JavaScript? Suppose I have three <div>s and I get a reference to one in JavaScript code, I want to get which is the next <div> and which is the previous. Answer Well in pure javascript my thinking is that you would first have to collate them inside a collection. So

Cross-browser way to determine whether a DOM event was cancelled

Is there a cross-browser way from a Javascript event handler to determine whether the event has been cancelled by a previous handler? IE and other browsers like Chrome etc. have an event.returnValue property that can be tested, but FireFox doesn’t appear to have any such property. Here’s an example of the scenario I’m talking about. You have a form like

Changing website favicon dynamically

I have a web application that’s branded according to the user that’s currently logged in. I’d like to change the favicon of the page to be the logo of the private label, but I’m unable to find any code or any examples of how to do this. Has anybody successfully done this before? I’m picturing having a dozen icons in

How can I change an element’s class with JavaScript?

How can I change the class of an HTML element in response to an onclick or any other events using JavaScript? Answer Modern HTML5 Techniques for changing classes Modern browsers have added classList which provides methods to make it easier to manipulate classes without needing a library: Unfortunately, these do not work in Internet Explorer prior to v10, though there

How to show a spinner while loading an image via JavaScript

I’m currently working on a web application which has a page which displays a single chart (a .png image). On another part of this page there are a set of links which, when clicked, the entire page reloads and looks exactly the same as before except for the chart in the middle of the page. What I want to do

Advertisement