0

Hy, its maybe a stupid question and tottaly easy, but I can't make it work so please help. Here's the code.

asp=height / width * 100;
document.getElementById("head_wrap").style["padding-top"] = "'+asp+%'"; 

using alert(asp); gives the correct value, but I made some mistake here "'+asp+%'", I want the value to be in percentages but as I said It doesn't work. thanks for your help!

2
  • The value is fine. The CSS property is wrong. It's paddingTop... Commented Sep 10, 2014 at 19:46
  • 2
    The value is also wrong because of quoting problems. Commented Sep 10, 2014 at 19:47

1 Answer 1

4

You have some syntax errors in the set value with double quotes and single quotes. You also could use a property from style object, for sample:

asp = height / width * 100;
document.getElementById("head_wrap").style.paddingTop = asp + "%"; 

Alternativally, you could use as you did, but with some adjusts in your code.

asp = height / width * 100;
document.getElementById("head_wrap").style["padding-top"] = asp + "%;

I modified your jsfiddle, take a look here.

See Also

Sign up to request clarification or add additional context in comments.

5 Comments

thanks but it still doesn't work the padding isn't added to the css of the element.here's the fiddle for better understanding what i want jsfiddle.net/hLp8g73n basically to mantain aspect ratio of backg. image and apply it to the parent div.
This is the syntax provided by javascript, make sure your DOM is right ot accept it. You also could try using pixels px instead %.
@TaoByebye: Your jsFiddle doesn't contain an element with ID head_wrap, so it can't work. Also, you are binding the window.onload handler after the the pages was already loaded, so it's not even triggered.
so what do you suggest instead to fix this window.onload problem
@TaoByebye: Either remove window.onload = function() { .... }; or set up the jsFiddle to run the script after the DOM is ready (you know, left hand column, no wrap - in <body>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.