getelementsbyclassname

How to remove a class from elements in pure JavaScript?

我的未来我决定 提交于 2019-11-27 07:44:58
I would like to know how to select all elements with class names "widget" and "hover" and then remove class "hover" from these elements. I have the following JavaScript code that selects all elements with class "widget" and "hover": var elements = document.getElementsByClassName('widget hover'); console.log(elements); This seems to work and outputs something like this (with no errors): [div#.widget... The problem is that if I try to remove the class "hover", I get an error: var elements = document.getElementsByClassName('widget hover'); console.log(elements); elements.classList.remove("hover")

GetElementsbyClassname: Open IE vs. MSXML2 Methods

馋奶兔 提交于 2019-11-27 07:13:25
问题 The following macro works just fine. It opens an instance of IE and uses the "getelementsbyclassname" method to return the expected value for "my_rate". However when I run the second macro which uses the "MSXML2" method, the macro fails on the noted line and a "Run-time error 438: Object doesn't support this property or method" error occurs. Why does the "Open IE" method work, but the "MSXML2" method fail with my code? I am running with IE 11. I also have a reference set to the Microsoft HTML

getElementsByClassName - Strange behavior

老子叫甜甜 提交于 2019-11-27 04:51:01
First, sorry for my bad English... I wrote a function to change the class of elements to change their properties. For some reason, only some of the elements have changed, it took me a few hours to find a solution, but he seems odd to me, perhaps you can explain to me. function replace(){ var elements = document.getElementsByClassName('classOne'); for (var i=0; i<elements.length;i++) { elements[i].className = 'classTwo'; } } not working as well, Demo here , [on chrome 25 and FF], So I took the loop coefficient: function replace(){ var elements = document.getElementsByClassName('classOne'); for

getElementsByClassName to change the style of elements when event occurs [duplicate]

守給你的承諾、 提交于 2019-11-27 03:31:33
问题 This question already has answers here : What do querySelectorAll and getElementsBy* methods return? (9 answers) Closed 3 years ago . I'm trying to use onmouseover="document.getElementsByClassName().style.background='color'" to change the color of all divs with a given classname to another color when hovering over another page element. Here is a jsfiddle -if anyone could give a few helpful pointers as to where I'm going wrong that would be great, I'm sure it's something really obvious that I

Get element inside element by class and ID - JavaScript

孤人 提交于 2019-11-27 03:05:24
Alright, I've dabbled in JavaScript before, but the most useful thing I've written is a CSS style-switcher. So I'm somewhat new to this. Let's say I have HTML code like this: <div id="foo"> <div class="bar"> Hello world! </div> </div> How would I change "Hello world!" to "Goodbye world!" ? I know how document.getElementByClass and document.getElementById work, but I would like to get more specific. Sorry if this has been asked before. Well, first you need to select the elements with a function like getElementById . var targetDiv = document.getElementById("foo").getElementsByClassName("bar")[0]

Get element inside element by class and ID - JavaScript

岁酱吖の 提交于 2019-11-26 10:19:19
问题 Alright, I\'ve dabbled in JavaScript before, but the most useful thing I\'ve written is a CSS style-switcher. So I\'m somewhat new to this. Let\'s say I have HTML code like this: <div id=\"foo\"> <div class=\"bar\"> Hello world! </div> </div> How would I change \"Hello world!\" to \"Goodbye world!\" ? I know how document.getElementByClass and document.getElementById work, but I would like to get more specific. Sorry if this has been asked before. 回答1: Well, first you need to select the

Strange behavior when iterating over HTMLCollection from getElementsByClassName

吃可爱长大的小学妹 提交于 2019-11-26 09:51:12
问题 I wrote a function to change the class of elements to change their properties. For some reason, only some of the elements have changed. It took me a few hours to find a solution, but it seems odd to me. Perhaps you can explain this to me. This isn’t working: function replace(){ var elements = document.getElementsByClassName(\'classOne\'); for (var i = 0; i < elements.length; i++) { elements[i].className = \'classTwo\'; } } See the JSFiddle: only every second item is affected; only every

JS: iterating over result of getElementsByClassName using Array.forEach

房东的猫 提交于 2019-11-26 00:27:52
问题 I want to iterate over some DOM elements, I\'m doing this: document.getElementsByClassName( \"myclass\" ).forEach( function(element, index, array) { //do stuff }); but I get an error: document.getElementsByClassName(\"myclass\").forEach is not a function I am using Firefox 3 so I know that both getElementsByClassName and Array.forEach are present. This works fine: [2, 5, 9].forEach( function(element, index, array) { //do stuff }); Is the result of getElementsByClassName an Array? If not, what

How to getElementByClass instead of GetElementById with JavaScript?

橙三吉。 提交于 2019-11-25 23:18:21
问题 I\'m trying to toggle the visibility of certain DIV elements on a website depending on the class of each DIV. I\'m using a basic JavaScript snippet to toggle them. The problem is that the script only uses getElementById , as getElementByClass is not supported in JavaScript. And unfortunately I do have to use class and not id to name the DIVs because the DIV names are dynamically generated by my XSLT stylesheet using certain category names. I know that certain browsers now support

What do querySelectorAll and getElementsBy* methods return?

被刻印的时光 ゝ 提交于 2019-11-25 21:29:42
问题 Do getElementsByClassName (and similar functions like getElementsByTagName and querySelectorAll ) work the same as getElementById or do they return an array of elements? The reason I ask is because I am trying to change the style of all elements using getElementsByClassName . See below. //doesn\'t work document.getElementsByClassName(\'myElement\').style.size = \'100px\'; //works document.getElementById(\'myIdElement\').style.size = \'100px\'; 回答1: Your getElementById() code works since IDs