How to change a html class with Javascript? [duplicate] - javascript

This question already has answers here:
How can I change an element's class with JavaScript?
(33 answers)
Closed 7 years ago.
I would like to Change a HTML class with JavaScript
HTML
<i id="test" class="fa">
How can I do it really simple?

Try this:
use .className to add and also get class name of particular element.
document.getElementById('test').className = "fa";

Aside from the className property, you can also use the much more elaborate classList property and its methods:
var ele = document.getElementById('test');
ele.classList.remove('fa');
ele.classList.add('ba');
You can also toggle() classes (with optional conditions!) and hence easily do without jQuery’s toggleClass() and its other class-related methods (like shown in the downvoted, accepted answer).

Try to use this in javascript:
$("#test").removeClass("fa");
$("#test").addClass("newClass");

Related

How do i replace an image on load that only has a title attribute? [duplicate]

This question already has answers here:
Find an element in DOM based on an attribute value
(11 answers)
Closed 5 years ago.
Lets say i have an Image in my html that has no id or class like:
<img title="me" src="something_blabla_nonactive">
and i want it to Change to
<img title="me" src="something_blabla_active">
when the page loads, how do i do that in JS?
Thanks in advance!
Use querySelector to get the first one like this:
var firstImg = document.querySelector('img[title="me"]');
Or querySelectorAll to get an array-like object of all the images that have the title attribute set to me like this:
var allImgs = document.querySelectorAll('img[title="me"]');
The selector [title="me"] is the CSS Attribute-Equal Selector, here is a reference of all CSS Selectors.
Use this:
$('img[title="me"]').attr("src", "something_blabla_activ")
$('img[title=blah]')
try this one.

jQuery: $(document).on(event, name as selector, function [duplicate]

This question already has answers here:
How can I select an element by name with jQuery?
(14 answers)
Closed 6 years ago.
Sorry if this is too simple, I am new to programming and I stumbled upon this problem. How can I use the name attribute as the selector inside a
$(document).on('click', '#thisisforid', function().. ?
It is well documented that I can use the id and class name by using #idname and .classname but it is nowhere to be found (I tried so hard to search) how to or if I could use the name attribute instead.
Use an Atrribute Selector like this:
$("[name = 'theNameYouWant']");
More CSS Selectors here.
Note: The CSS Selectors are not exclusive features of jQuery. They work everywhere (in CSS files, using document.querySelector ...)
Try this: $(document).on('click', '*[name="3"]', function().. the * should not be necessary in most cases.
You can do it this way.
$( "[name*='man']" ).on('click', function(){
alert("worked!!");
})
You can refer this docs for attribute selection in jQuery
https://api.jquery.com/attribute-contains-selector/

Change class style (not individual elements) that's not in stylesheet using Javascript [duplicate]

This question already has answers here:
How to dynamically create CSS class in JavaScript and apply?
(17 answers)
Closed 6 years ago.
I have a series of divs that share a class for assorted reasons (and a number of divs that get created via code at other points).
I've tried to find answers, but all rely on the class being in the stylesheet (if there even is one), which is something that doesn't happen in this case or that alters existing elements instead of the rules themselves.
So I need to do something like...
var add_to_styles('myClassName');
myClassName.cssText = "color: red;"
And have all divs that get created (both before and after the script running) that have the class
<div class="myClassName">I should be red</div>
To come out with the rule change. But I haven't found anything like this.
You can add new <style></style> with new rules (new or existing classes) and append it to body (so it will be read after other stylesheets).
You can also target by specific parent (and don't modify other existing elements if you don't want to change all of them).
If you are using jQuery you can try to use .css() function.
https://jsfiddle.net/rixlabs/annt81df/1/
for some tests
$(".myClassName").css("color", "red");
Try this one:
document.getElementByClassName('myClassName').style.color = "red";
or
document.getElementById('myIdName').style.color = "red";

Editing an item's style via JavaScript? [duplicate]

This question already has answers here:
Changing CSS Values with Javascript
(9 answers)
Closed 8 years ago.
I'm looking for help regarding how to edit an object's style in a separate CSS file via JS. For example, if I have an object that I style with #objectid {left: 0%;} in my CSS file, how would I go about changing that left property via JavaScript? I'm aware you can do object.style.property but that hasn't been working for me as of late. What's the most efficient/easy method of doing this? Thanks in advance.
You have to get the element in the DOM
with pure javascript:
document.getElementById('objectid').style.left = '20%'
With jQuery
$('#objectid').css('left', '20%');
or
$('#objectid').css({'left': '20%'});
The standard way:
document.getElementById('objectid').style.left = "10%";
But if you're worried about efficiency, try not to modify any styles directly using JS... try adding/removing classes instead.
This is correct:
document.getElementById('thing').style.left="5px"
But I believe you need to set the position before it can work, e.g.:
document.getElementById('thing').style.position="absolute";
document.getElementById('thing').style.left="5px"

Using :not() with attributes [duplicate]

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();

Categories