how to append a css class to an element by javascript?
Tags: javascript,css
Problem :
Suppose a HTML element's id
is known, so the element can be refereced using:
document.getElementById(element_id);
Does a native Javascript function exist that can be used to append a CSS class to that element?
Solution :
var element = document.getElementById(element_id);
element.className += " " + newClassName;
Voilà. This will work on pretty much every browser ever. The leading space is important, because the className
property treats the css classes like a single string, which ought to match the class
attribute on HTML elements (where multiple classes must be separated by spaces).
Incidentally, you're going to be better off using a Javascript library like prototype or jQuery, which have methods to do this, as well as functions that can first check if an element already has a class assigned.
In prototype, for instance:
// Prototype automatically checks that the element doesn't already have the class
$(element_id).addClassName(newClassName);
See how much nicer that is?!
CSS Howto..
You should make it look something like... ul.image-list li:hover img.grayscale { filter: grayscale(0%); -webkit-filter: grayscale(0%); filter: none; } ul.image-list li:hover span.text-content { opacity: 1; } So far, the rule you...
Does this help? .page--game-core_bottom .tabs li { position: relative; /* Set as a reference point */ } .page--game-core_bottom .tabs li span{ /* margin-top: 10px; */ position: absolute; bottom: 0; left:...
Hey, to prevent the highlighting you can use CSS: * { -webkit-user-select: none; -moz-user-select: none; user-select: none; } To prevent default actions (which may be the cause of your cursor...
I don't know the HTC file in question (a link to where you got it from would be a handy edit to the question), but I am familiar with other...
Using min-width and width on a single selector is fine, it's often the basis for most fluid layouts. However, IE6 lacks support for the min-width property; instead, it interprets the...
Making things look identical in all browsers isn't necessary - the main objective is to get things not to break in other browsers. There are certain things in different browsers...
This can be done only through the configurator. But you can try this: Add attr (data-chrome="transparent") <a class="twitter-timeline" href="https://twitter.com/google" data-widget-id="405388412095438849" data-chrome="transparent">Tweets by @google</a> And then you may make somthing like...
Do this: .stack-panel.vertical .stack-slot { clear:left; float:left; } jsFiddle example No <br> tags, they stack vertically, and shrink to fit their content....
You can always avoid the issue by using a relative/rooted path: <link rel="stylesheet" href="/css/screen.css"> If you must use a full URL, I'm not sure why you can't use the https...
Normally you are creating XSL FO from XML and XSL and that would not apply because you would do something in an template-match for whatever creates your table-row. So you...
It seems most others have not quite understood the gist of your post. I'll break it down for you: CSS positiong is complex because it was designed by many different...
first of all i would reset the margin and padding of the whole page: * { margin: 0; padding: 0; } More info. This way some default paddings and margins...
Someone has mentioned flexbox, which while a perfectly valid solution may not be what you need depending on your minimum browser requirements as flexbox is still not as well supported...
@media screen and (-webkit-min-device-pixel-ratio:0) { .div-special{ border-bottom: 1px solid #c8c8c8; } } ...
After hours of searching I found out the answer be adjusting the css. Now it's working perfectly with the code below: .ui-accordion-content { overflow:visible !important; } ...
After some research, I've found out that this is actually intended behaviour, due to privacy limitations. You may style: Color Background-color Border-color Outline color Here's an article on the subject:...
Specificity can be omitted by not nesting elements in divs, and instead just giving elements themselves unique identifiers. <div id="mylist"> <ul> <li>ListItem1</li> <li>ListItem2</li> </ul> </div> Replace with <ul id="mylist"> <li>ListItem1</li>...
I'm not sure which framework did you mean by saying that "some classes are define in the framework as:". In Less, you don't declare mixins the way you showed it....
unfortunately, as i said before: using css, you can only select an element that comes after the one you clicked, not before and not a parent. you can work around...
Try setting the min-width and min-height of the divs if you want them to stay a specific size regardless of the screen size.
Create a cascade of style sheets like so: <link id="stylesheet1" rel="stylesheet" href="css/style.css" type="text/css" media="all" / <!--[if IE]> <link id="stylesheet2" rel="stylesheet" href="css/ie.css" type="text/css" media="all" /> <![endif]--> <!--[if lte IE 6]> <link...
It sure seems to have been asked before (2003), and before (2002), or before (2005) The last link actually suggest a javascript-based solution, but for a fixed (i.e. non fluid)...
Use $(".mutat").click(function() { $(this).hide() // hide the clicked link .closest('.sidebar_szuro_div').find(".filter_div") // find the .filter_div only under the same .sidebar_szuro_div .removeClass('filter_div'); //remove class on them }); $(function() { $(".mutat").click(function() { $(this).hide()...
Try to pass "slow"/"fast"/"medium"/any delay in milliseconds as a parameter to show, $('[class^="question"]').on('click', function(e){ e.preventDefault(); var numb = this.className.replace('question', ''); $('[id^="answer"]').hide(); $('#answer' + numb).stop().show("slow"); }); DEMO Or by using .fadeIn()...
I would accomplish this as follows: HTML: <div class="round"> <img src="http://www.somebodymarketing.com/wp-content/uploads/2013/05/Stock-Dock-House.jpg"/> </div> CSS: .round{ width:10%; padding-top:10%; overflow:hidden; position:relative; border-radius:50%; } img{ position:absolute; top:50%; left:50%;...
It's impossible to say from your question what's left behind when all instances of .gridBox disappear from their parent .mainCont element, but the solution would be to put all of...
You should just use a single gradient like in the below snippet with the start and end as transparent. Explanation: transparent 0% means the gradient starts with transparent color #8C8C8C...
This will do the job: CSS .rotated { -webkit-transform: rotate(-90deg); -webkit-transform-origin:left top; -moz-transform: rotate(-90deg); -moz-transform-origin:left top; -ms-transform: rotate(-90deg); -ms-transform-origin:left top; -o-transform: rotate(-90deg); -o-transform-origin:left top; transform: rotate(-90deg);...
Try adding a min-width attribute as well. <link rel='stylesheet' media='only screen and (min-width: 321px) and (max-width: 360px)' href='css/design360.css' /> Right now your second and third stylesheets will apply to any...
Try decorating the property with the Display Format attribute: //Your view object [DisplayFormat( DataFormatString= "{0:F}" )] public Double Money { get; set; } //Your view code <%: Html.TextBoxFor(model => model.Money,...