how to add string to the class javascript(jquery)


Tags: javascript,jquery,css

Problem :

I have the class and the string. How can I put this string from javascript to css?

/*css*/
.new{
}
/*javascript*/
var k='margin-top: 10px;';

How can I put the string k into class new?



Solution :

Perhaps you want something like this?

fiddle

HTML

<div class="new">
    <input id="amount">
</div>

CSS

.new {
    margin-top: 10px;
    background-color: blue;
}

JS

$("#amount").keyup(function(){
    $(".new").css("margin-top", parseInt($("#amount").val()))
}).keyup();

This changes the margin-top for whichever elements with the class .new whenever you type something into the input box. If you don't want user input, you can just use $(".new").css("margin-top", yourValue);


    CSS Howto..

    How do I make my footer sit at the bottom of my content?

    How to style more than one ul

    How to achieve “background-image: cover” CSS effect for dynamic images?

    How are new lines made with text as background?

    How to implement a drag and swap effect in CSS and JS [closed]

    CSS: How to align text to the center of the image (left side)

    How to add background video stop button

    CSS Sprites - How to leave Hover button in depressed state

    How do I get the color attribute of the returned div?

    How to draw rectangle on the website to highlight?

    How to automatically put DIVs in columns (unknown number of DIVs)

    Flexbox: How do I create my thumbnails to span 100% of the container width?

    How Do you combine CSS code to an HTML code? I want to Combine them? [closed]

    JQuery: Creating boxed text that shows on top of a div on hover

    How to create such layout in CSS?

    How do I apply CSS styles to Raphael.js objects using jQuery?

    CSS: How to fix overlapping divs

    How to make css background-blend-mode work with gradients?

    How can I use css files in django?

    How to have CSS animation NOT consume one's entire CPU?

    Jquery: How to make the content showup below the navigation bar when the anchor link is clicked?

    How to bypass an overriding CSS Declaration

    How I can add td to css style for inputs and its label?

    How to make div expand all the way to the right

    How can I prevent styles from applying to certain HTML snippets?

    CSS: how to make background on left and right of the content

    How to get css 3 column layout with floats and no margins?

    How do I center my content inside the menu?

    In a html table cell, how can i force images to be the bottom left and the bottom right that have different heights?

    how to write a desktop application which uses HTML and CSS for user interface and python/perl/c++/java for the processing?