2

I have follwing html, css and js

HTML

<a href="#" class="name">name</a>

CSS

a {
    position: relative;
}

JS

var styleString = 'content:" ";, width: 0, height: 0;, border-style: solid;, border-width: 100px 100px 0 100px;, border-color: #007bff transparent transparent transparent;, position: absolute;';

document.styleSheets[0].addRule('.name:after', styleString);

DEMO

My question is only the content: " " is getting added and the rest of the rules are missing. What is the wrong with my code?

4
  • 1
    remove commas and use semicolons to seperate attributes Commented Aug 26, 2015 at 5:57
  • 4
    Why do you add a comma after each semicolon? Extra comma I would say. Commented Aug 26, 2015 at 5:59
  • @user4759923 wow thanks i was acting stupid thanks Commented Aug 26, 2015 at 6:04
  • 1
    you'd be better off adding the css selector and the set of styes to the css file then using js to add and remove a class as required, then your css rules are easier to re-use and your js is a lot less cluttered Commented Aug 26, 2015 at 6:47

1 Answer 1

2

As @MoshFeu has already pointed out, the commas after each ; are not necessary and should be removed:

The full code:

var styleString = 'content:" "; width: 0; height: 0; border-style: solid; border-width: 100px 100px 0 100px; border-color: #007bff transparent transparent transparent; position: absolute;';

document.styleSheets[0].addRule('.name:after', styleString);
Sign up to request clarification or add additional context in comments.

2 Comments

This code will not work too. He needs to remove the ,
Thanks friend. The css is for a css triangle. the width is not needed so it should be 0. I figured that the problem with my code was that extra , as @MoshFeu pointed.. :)

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.