I hav a certain style sheet for a div. Now i want to modify one attribute of div dynamically using js.
How can i do it?
document.getElementById("xyz").style.padding-top = "10px";
Is this correct?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s almost correct.
Since the
-is a javascript operator, you can’t really have that in property names. If you were setting,borderor something single-worded like that instead, your code would work just fine.However, the thing you need to remember for
padding-top, and for any hyphenated attribute name, is that in javascript, you remove the hyphen, and make the next letter uppercase, so in your case that’d bepaddingTop.There are some other exceptions. JavaScript has some reserved words, so you can’t set
floatlike that, for instance. Instead, in some browsers you need to usecssFloatand in othersstyleFloat. It is for discrepancies like this that it is recommended that you use a framework such as jQuery, that handles browser incompatibilities for you…