0

document.getElementById("Id1").style.backgroundImage = "url(value)";

to change the background image attribute in the CSS. But, the attribute used in CSS is

background-Image

Why we can't use the same name under Javascript as document.getElementById("Id1").style.background-Image = "url(value)";

8
  • I recommend you to use jQuery instead of javascript Commented Jul 29, 2016 at 5:41
  • Have a look at this: <stackoverflow.com/questions/512054/…> Maybe this can help? Commented Jul 29, 2016 at 5:42
  • If you want I can show you how jQuery works Commented Jul 29, 2016 at 5:42
  • 1
    @eronax59 You're joking right? Commented Jul 29, 2016 at 5:43
  • @4castle its only for you dear Commented Jul 29, 2016 at 5:44

1 Answer 1

2

As @Bitwise Creative said, you can't use - out of string. Instead you can do this,

document.getElementById("Id1").style["background-image"] = "url(value)";

If you use document.getElementById("Id1").style.background-Image = "url(value)", what will happen means javascript consider as statement before '-' "document.getElementById("Id1").style.background" and image consider as variable. So you will get syntax error

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

Comments

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.