1

I'm trying to add a inline-style using JavaScript as soon as the site is loaded (can't use CSS directly due to reasons). I want all text within a table to be horizontally centered,

This is my JavaScript code so far:

window.onload = center_content();
function center_content (){
    var all_td = document.getElementsByTagName('table');
    all_td[0].style.textAlign = "center !important";
}

Edit: Code corrected so far

jsfiddle: http://jsfiddle.net/GinSan/h46mz6na/4/ (remember that I can't directly use CSS in this case)

Any help is much appreciated. No jQuery solutions please.

1
  • 1
    all_td[0].style.text-align = "center !important"; is interpreted as all_td[0].style.text - align = "center !important";, i.e. it tries to subtract align from all_td[0].style.text. Then you are trying to assign a value to that value, which doesn't make sense to the engine. Commented Aug 19, 2014 at 23:29

2 Answers 2

4

Hyphen-separated properties of style object should be used in camelCase when it comes to JavaScript.

Try using so:

document.getElementsByTagName('table')[0].style.textAlign = "center";

You could also use .style["text-align"] to achieve the same result.

However if you want to set the priority (i.e. !important keyword), not only the value, you could use .setProperty() method as follows:

EXAMPLE HERE

document.getElementsByTagName('table')[0].style.setProperty("text-align", "center", "important");

And one more thing, by using () at window.onload = center_content(); you're executing center_content() immediately and assigning the returned value which is undefined to window.onload.

You could either do it like window.onload = center_content; or use an anonymous function:

window.onload = function () { /* ... */ };
Sign up to request clarification or add additional context in comments.

6 Comments

"Hyphen-separated properties should be used in camelCase when it comes to JavaScript." But note that that only works for the properties of the element.style object. See also developer.mozilla.org/en-US/docs/Web/CSS/…
@FelixKling Of course! I meant the style properties. I just forgot to mention it. Thanks anyway.
Thanks, firefox doesn't show the error message anymore. However, the text still doesnt appear centered °v° Maybe there's another error somewhere, I added a fiddle
@Gin-San Your fiddle gives me a 404 error. I added mine. Check the update please.
Sorry, don't know why the link didn't work anymore. Tried your example, but for some reason, it doesn't work, don't know why :/ I added a functional fiddle link, could you take a look at it?
|
0

You cannot use - in a variable name or property. Instead use style.textAlign

1 Comment

You can use - in a property (name). You just can't use dot notation to access that property.

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.