Add CSS to a JavaScript button












1














On my project I use JS Buttons a lot, I can't find a way to add CSS to them. Can you help?



leftBtn = document.createElement('button');
leftBtn.innerText = "<";
rightBtn = document.createElement('button');
rightBtn.innerText = ">";
fireBtn = document.createElement('button');
fireBtn.innerText = "*";


Using CSS classes to change buttons:
<button class="btn">default button</button>



Class used










share|improve this question
























  • Simply add the CSS property you want like leftBtn.marginLeft = "20px".....
    – Mamun
    Nov 12 at 14:07










  • Does this work with classes?
    – Auron Hines
    Nov 12 at 14:08
















1














On my project I use JS Buttons a lot, I can't find a way to add CSS to them. Can you help?



leftBtn = document.createElement('button');
leftBtn.innerText = "<";
rightBtn = document.createElement('button');
rightBtn.innerText = ">";
fireBtn = document.createElement('button');
fireBtn.innerText = "*";


Using CSS classes to change buttons:
<button class="btn">default button</button>



Class used










share|improve this question
























  • Simply add the CSS property you want like leftBtn.marginLeft = "20px".....
    – Mamun
    Nov 12 at 14:07










  • Does this work with classes?
    – Auron Hines
    Nov 12 at 14:08














1












1








1







On my project I use JS Buttons a lot, I can't find a way to add CSS to them. Can you help?



leftBtn = document.createElement('button');
leftBtn.innerText = "<";
rightBtn = document.createElement('button');
rightBtn.innerText = ">";
fireBtn = document.createElement('button');
fireBtn.innerText = "*";


Using CSS classes to change buttons:
<button class="btn">default button</button>



Class used










share|improve this question















On my project I use JS Buttons a lot, I can't find a way to add CSS to them. Can you help?



leftBtn = document.createElement('button');
leftBtn.innerText = "<";
rightBtn = document.createElement('button');
rightBtn.innerText = ">";
fireBtn = document.createElement('button');
fireBtn.innerText = "*";


Using CSS classes to change buttons:
<button class="btn">default button</button>



Class used







javascript css button






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 14:30

























asked Nov 12 at 14:05









Auron Hines

83




83












  • Simply add the CSS property you want like leftBtn.marginLeft = "20px".....
    – Mamun
    Nov 12 at 14:07










  • Does this work with classes?
    – Auron Hines
    Nov 12 at 14:08


















  • Simply add the CSS property you want like leftBtn.marginLeft = "20px".....
    – Mamun
    Nov 12 at 14:07










  • Does this work with classes?
    – Auron Hines
    Nov 12 at 14:08
















Simply add the CSS property you want like leftBtn.marginLeft = "20px".....
– Mamun
Nov 12 at 14:07




Simply add the CSS property you want like leftBtn.marginLeft = "20px".....
– Mamun
Nov 12 at 14:07












Does this work with classes?
– Auron Hines
Nov 12 at 14:08




Does this work with classes?
– Auron Hines
Nov 12 at 14:08












7 Answers
7






active

oldest

votes


















2














You could use this code to add a css class to each button:



 leftBtn.className = "leftOne";
rightBtn.className = "rightOne";
fireBtn.className = "fireOne";


Then you can use regular css to style them.






share|improve this answer





















  • I want something like this: <button class="btn">default button</button> except I can use class="btn" on it so it has the CSS style of the button
    – Auron Hines
    Nov 12 at 14:16










  • then in my example just change leftOne, rightOne and fireOne to btn and then each will have the btn css class assigned to it.
    – Ron C
    Nov 12 at 14:18










  • leftBtn.className = "btn-primary"; rightBtn.className = "btn-primary"; fireBtn.className = "btn-primary"; Doesn't work. (picturepan2.github.io/spectre/elements/buttons.html)
    – Auron Hines
    Nov 12 at 14:27












  • ?? Doing so assigns the "btn-primary" css class to each button an allows you to style the button by using <style> .btn-primary {/*some styles here*/}; </style> What doesn't work about it?
    – Ron C
    Nov 12 at 14:30








  • 1




    I see on the page you linked to that you need to use "btn btn-primary" so use leftBtn.className = "btn btn-primary"; rightBtn.className = "btn btn-primary"; fireBtn.className = "btn btn-primary";
    – Ron C
    Nov 12 at 14:32



















2














You could add a class to your buttons, and then add any styling in CSS:



leftBtn.className = "class_name"





share|improve this answer





























    1














    You probably need to use the className to your Javascript:



    EXAMPLE:



    leftBtn.className = "name";


    Hope this helps :)






    share|improve this answer





























      1














      you can use for example :



      leftBtn.style.color = "blue";


      Lear more about in https://developer.mozilla.org/es/docs/Web/API/HTMLElement/style






      share|improve this answer





























        1














        In general, there are two main ways to do that.



        The first one using inline styles, just keep in mind that inline style has the highest precedence in a document.



        elt.style.color = '...'


        or



        elt.setAttribute('style', '...')


        More info here



        The second is by using the class name. You can simply define a class name and write CSS for this class name.



        element.className.add = 'your-class-name'


        then



        .your-class-name { color: red }


        In a second way, you can manage class names by using methods like add, remove, toggle



        More info here






        share|improve this answer





























          0














          Just do something like this



          leftBtn.style.marginLeft = '20px'
          rightBtn.style.marginLeft = '20px'


          I guess it






          share|improve this answer





























            0














            var leftBtn = document.createElement('button');
            leftBtn.innerText = "<";
            leftBtn.className = "test_style"; // Set class name
            leftBtn.style.color = "#000"; // You can also set style properties inline


            ...



            Here is a playground:



            https://jsfiddle.net/u5hojsgb/






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263825%2fadd-css-to-a-javascript-button%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              7 Answers
              7






              active

              oldest

              votes








              7 Answers
              7






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              You could use this code to add a css class to each button:



               leftBtn.className = "leftOne";
              rightBtn.className = "rightOne";
              fireBtn.className = "fireOne";


              Then you can use regular css to style them.






              share|improve this answer





















              • I want something like this: <button class="btn">default button</button> except I can use class="btn" on it so it has the CSS style of the button
                – Auron Hines
                Nov 12 at 14:16










              • then in my example just change leftOne, rightOne and fireOne to btn and then each will have the btn css class assigned to it.
                – Ron C
                Nov 12 at 14:18










              • leftBtn.className = "btn-primary"; rightBtn.className = "btn-primary"; fireBtn.className = "btn-primary"; Doesn't work. (picturepan2.github.io/spectre/elements/buttons.html)
                – Auron Hines
                Nov 12 at 14:27












              • ?? Doing so assigns the "btn-primary" css class to each button an allows you to style the button by using <style> .btn-primary {/*some styles here*/}; </style> What doesn't work about it?
                – Ron C
                Nov 12 at 14:30








              • 1




                I see on the page you linked to that you need to use "btn btn-primary" so use leftBtn.className = "btn btn-primary"; rightBtn.className = "btn btn-primary"; fireBtn.className = "btn btn-primary";
                – Ron C
                Nov 12 at 14:32
















              2














              You could use this code to add a css class to each button:



               leftBtn.className = "leftOne";
              rightBtn.className = "rightOne";
              fireBtn.className = "fireOne";


              Then you can use regular css to style them.






              share|improve this answer





















              • I want something like this: <button class="btn">default button</button> except I can use class="btn" on it so it has the CSS style of the button
                – Auron Hines
                Nov 12 at 14:16










              • then in my example just change leftOne, rightOne and fireOne to btn and then each will have the btn css class assigned to it.
                – Ron C
                Nov 12 at 14:18










              • leftBtn.className = "btn-primary"; rightBtn.className = "btn-primary"; fireBtn.className = "btn-primary"; Doesn't work. (picturepan2.github.io/spectre/elements/buttons.html)
                – Auron Hines
                Nov 12 at 14:27












              • ?? Doing so assigns the "btn-primary" css class to each button an allows you to style the button by using <style> .btn-primary {/*some styles here*/}; </style> What doesn't work about it?
                – Ron C
                Nov 12 at 14:30








              • 1




                I see on the page you linked to that you need to use "btn btn-primary" so use leftBtn.className = "btn btn-primary"; rightBtn.className = "btn btn-primary"; fireBtn.className = "btn btn-primary";
                – Ron C
                Nov 12 at 14:32














              2












              2








              2






              You could use this code to add a css class to each button:



               leftBtn.className = "leftOne";
              rightBtn.className = "rightOne";
              fireBtn.className = "fireOne";


              Then you can use regular css to style them.






              share|improve this answer












              You could use this code to add a css class to each button:



               leftBtn.className = "leftOne";
              rightBtn.className = "rightOne";
              fireBtn.className = "fireOne";


              Then you can use regular css to style them.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 12 at 14:08









              Ron C

              9,13663873




              9,13663873












              • I want something like this: <button class="btn">default button</button> except I can use class="btn" on it so it has the CSS style of the button
                – Auron Hines
                Nov 12 at 14:16










              • then in my example just change leftOne, rightOne and fireOne to btn and then each will have the btn css class assigned to it.
                – Ron C
                Nov 12 at 14:18










              • leftBtn.className = "btn-primary"; rightBtn.className = "btn-primary"; fireBtn.className = "btn-primary"; Doesn't work. (picturepan2.github.io/spectre/elements/buttons.html)
                – Auron Hines
                Nov 12 at 14:27












              • ?? Doing so assigns the "btn-primary" css class to each button an allows you to style the button by using <style> .btn-primary {/*some styles here*/}; </style> What doesn't work about it?
                – Ron C
                Nov 12 at 14:30








              • 1




                I see on the page you linked to that you need to use "btn btn-primary" so use leftBtn.className = "btn btn-primary"; rightBtn.className = "btn btn-primary"; fireBtn.className = "btn btn-primary";
                – Ron C
                Nov 12 at 14:32


















              • I want something like this: <button class="btn">default button</button> except I can use class="btn" on it so it has the CSS style of the button
                – Auron Hines
                Nov 12 at 14:16










              • then in my example just change leftOne, rightOne and fireOne to btn and then each will have the btn css class assigned to it.
                – Ron C
                Nov 12 at 14:18










              • leftBtn.className = "btn-primary"; rightBtn.className = "btn-primary"; fireBtn.className = "btn-primary"; Doesn't work. (picturepan2.github.io/spectre/elements/buttons.html)
                – Auron Hines
                Nov 12 at 14:27












              • ?? Doing so assigns the "btn-primary" css class to each button an allows you to style the button by using <style> .btn-primary {/*some styles here*/}; </style> What doesn't work about it?
                – Ron C
                Nov 12 at 14:30








              • 1




                I see on the page you linked to that you need to use "btn btn-primary" so use leftBtn.className = "btn btn-primary"; rightBtn.className = "btn btn-primary"; fireBtn.className = "btn btn-primary";
                – Ron C
                Nov 12 at 14:32
















              I want something like this: <button class="btn">default button</button> except I can use class="btn" on it so it has the CSS style of the button
              – Auron Hines
              Nov 12 at 14:16




              I want something like this: <button class="btn">default button</button> except I can use class="btn" on it so it has the CSS style of the button
              – Auron Hines
              Nov 12 at 14:16












              then in my example just change leftOne, rightOne and fireOne to btn and then each will have the btn css class assigned to it.
              – Ron C
              Nov 12 at 14:18




              then in my example just change leftOne, rightOne and fireOne to btn and then each will have the btn css class assigned to it.
              – Ron C
              Nov 12 at 14:18












              leftBtn.className = "btn-primary"; rightBtn.className = "btn-primary"; fireBtn.className = "btn-primary"; Doesn't work. (picturepan2.github.io/spectre/elements/buttons.html)
              – Auron Hines
              Nov 12 at 14:27






              leftBtn.className = "btn-primary"; rightBtn.className = "btn-primary"; fireBtn.className = "btn-primary"; Doesn't work. (picturepan2.github.io/spectre/elements/buttons.html)
              – Auron Hines
              Nov 12 at 14:27














              ?? Doing so assigns the "btn-primary" css class to each button an allows you to style the button by using <style> .btn-primary {/*some styles here*/}; </style> What doesn't work about it?
              – Ron C
              Nov 12 at 14:30






              ?? Doing so assigns the "btn-primary" css class to each button an allows you to style the button by using <style> .btn-primary {/*some styles here*/}; </style> What doesn't work about it?
              – Ron C
              Nov 12 at 14:30






              1




              1




              I see on the page you linked to that you need to use "btn btn-primary" so use leftBtn.className = "btn btn-primary"; rightBtn.className = "btn btn-primary"; fireBtn.className = "btn btn-primary";
              – Ron C
              Nov 12 at 14:32




              I see on the page you linked to that you need to use "btn btn-primary" so use leftBtn.className = "btn btn-primary"; rightBtn.className = "btn btn-primary"; fireBtn.className = "btn btn-primary";
              – Ron C
              Nov 12 at 14:32













              2














              You could add a class to your buttons, and then add any styling in CSS:



              leftBtn.className = "class_name"





              share|improve this answer


























                2














                You could add a class to your buttons, and then add any styling in CSS:



                leftBtn.className = "class_name"





                share|improve this answer
























                  2












                  2








                  2






                  You could add a class to your buttons, and then add any styling in CSS:



                  leftBtn.className = "class_name"





                  share|improve this answer












                  You could add a class to your buttons, and then add any styling in CSS:



                  leftBtn.className = "class_name"






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 14:08









                  McVenco

                  1,01011528




                  1,01011528























                      1














                      You probably need to use the className to your Javascript:



                      EXAMPLE:



                      leftBtn.className = "name";


                      Hope this helps :)






                      share|improve this answer


























                        1














                        You probably need to use the className to your Javascript:



                        EXAMPLE:



                        leftBtn.className = "name";


                        Hope this helps :)






                        share|improve this answer
























                          1












                          1








                          1






                          You probably need to use the className to your Javascript:



                          EXAMPLE:



                          leftBtn.className = "name";


                          Hope this helps :)






                          share|improve this answer












                          You probably need to use the className to your Javascript:



                          EXAMPLE:



                          leftBtn.className = "name";


                          Hope this helps :)







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 12 at 14:08









                          Ben Swindells

                          1829




                          1829























                              1














                              you can use for example :



                              leftBtn.style.color = "blue";


                              Lear more about in https://developer.mozilla.org/es/docs/Web/API/HTMLElement/style






                              share|improve this answer


























                                1














                                you can use for example :



                                leftBtn.style.color = "blue";


                                Lear more about in https://developer.mozilla.org/es/docs/Web/API/HTMLElement/style






                                share|improve this answer
























                                  1












                                  1








                                  1






                                  you can use for example :



                                  leftBtn.style.color = "blue";


                                  Lear more about in https://developer.mozilla.org/es/docs/Web/API/HTMLElement/style






                                  share|improve this answer












                                  you can use for example :



                                  leftBtn.style.color = "blue";


                                  Lear more about in https://developer.mozilla.org/es/docs/Web/API/HTMLElement/style







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Nov 12 at 14:10









                                  Matias Simone

                                  154




                                  154























                                      1














                                      In general, there are two main ways to do that.



                                      The first one using inline styles, just keep in mind that inline style has the highest precedence in a document.



                                      elt.style.color = '...'


                                      or



                                      elt.setAttribute('style', '...')


                                      More info here



                                      The second is by using the class name. You can simply define a class name and write CSS for this class name.



                                      element.className.add = 'your-class-name'


                                      then



                                      .your-class-name { color: red }


                                      In a second way, you can manage class names by using methods like add, remove, toggle



                                      More info here






                                      share|improve this answer


























                                        1














                                        In general, there are two main ways to do that.



                                        The first one using inline styles, just keep in mind that inline style has the highest precedence in a document.



                                        elt.style.color = '...'


                                        or



                                        elt.setAttribute('style', '...')


                                        More info here



                                        The second is by using the class name. You can simply define a class name and write CSS for this class name.



                                        element.className.add = 'your-class-name'


                                        then



                                        .your-class-name { color: red }


                                        In a second way, you can manage class names by using methods like add, remove, toggle



                                        More info here






                                        share|improve this answer
























                                          1












                                          1








                                          1






                                          In general, there are two main ways to do that.



                                          The first one using inline styles, just keep in mind that inline style has the highest precedence in a document.



                                          elt.style.color = '...'


                                          or



                                          elt.setAttribute('style', '...')


                                          More info here



                                          The second is by using the class name. You can simply define a class name and write CSS for this class name.



                                          element.className.add = 'your-class-name'


                                          then



                                          .your-class-name { color: red }


                                          In a second way, you can manage class names by using methods like add, remove, toggle



                                          More info here






                                          share|improve this answer












                                          In general, there are two main ways to do that.



                                          The first one using inline styles, just keep in mind that inline style has the highest precedence in a document.



                                          elt.style.color = '...'


                                          or



                                          elt.setAttribute('style', '...')


                                          More info here



                                          The second is by using the class name. You can simply define a class name and write CSS for this class name.



                                          element.className.add = 'your-class-name'


                                          then



                                          .your-class-name { color: red }


                                          In a second way, you can manage class names by using methods like add, remove, toggle



                                          More info here







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Nov 12 at 14:43









                                          Eugene Dzhevadov

                                          1064




                                          1064























                                              0














                                              Just do something like this



                                              leftBtn.style.marginLeft = '20px'
                                              rightBtn.style.marginLeft = '20px'


                                              I guess it






                                              share|improve this answer


























                                                0














                                                Just do something like this



                                                leftBtn.style.marginLeft = '20px'
                                                rightBtn.style.marginLeft = '20px'


                                                I guess it






                                                share|improve this answer
























                                                  0












                                                  0








                                                  0






                                                  Just do something like this



                                                  leftBtn.style.marginLeft = '20px'
                                                  rightBtn.style.marginLeft = '20px'


                                                  I guess it






                                                  share|improve this answer












                                                  Just do something like this



                                                  leftBtn.style.marginLeft = '20px'
                                                  rightBtn.style.marginLeft = '20px'


                                                  I guess it







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Nov 12 at 14:13









                                                  Igor Bezsmertnyi

                                                  1




                                                  1























                                                      0














                                                      var leftBtn = document.createElement('button');
                                                      leftBtn.innerText = "<";
                                                      leftBtn.className = "test_style"; // Set class name
                                                      leftBtn.style.color = "#000"; // You can also set style properties inline


                                                      ...



                                                      Here is a playground:



                                                      https://jsfiddle.net/u5hojsgb/






                                                      share|improve this answer


























                                                        0














                                                        var leftBtn = document.createElement('button');
                                                        leftBtn.innerText = "<";
                                                        leftBtn.className = "test_style"; // Set class name
                                                        leftBtn.style.color = "#000"; // You can also set style properties inline


                                                        ...



                                                        Here is a playground:



                                                        https://jsfiddle.net/u5hojsgb/






                                                        share|improve this answer
























                                                          0












                                                          0








                                                          0






                                                          var leftBtn = document.createElement('button');
                                                          leftBtn.innerText = "<";
                                                          leftBtn.className = "test_style"; // Set class name
                                                          leftBtn.style.color = "#000"; // You can also set style properties inline


                                                          ...



                                                          Here is a playground:



                                                          https://jsfiddle.net/u5hojsgb/






                                                          share|improve this answer












                                                          var leftBtn = document.createElement('button');
                                                          leftBtn.innerText = "<";
                                                          leftBtn.className = "test_style"; // Set class name
                                                          leftBtn.style.color = "#000"; // You can also set style properties inline


                                                          ...



                                                          Here is a playground:



                                                          https://jsfiddle.net/u5hojsgb/







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Nov 12 at 14:35









                                                          Vlado

                                                          1,0271011




                                                          1,0271011






























                                                              draft saved

                                                              draft discarded




















































                                                              Thanks for contributing an answer to Stack Overflow!


                                                              • Please be sure to answer the question. Provide details and share your research!

                                                              But avoid



                                                              • Asking for help, clarification, or responding to other answers.

                                                              • Making statements based on opinion; back them up with references or personal experience.


                                                              To learn more, see our tips on writing great answers.





                                                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                              Please pay close attention to the following guidance:


                                                              • Please be sure to answer the question. Provide details and share your research!

                                                              But avoid



                                                              • Asking for help, clarification, or responding to other answers.

                                                              • Making statements based on opinion; back them up with references or personal experience.


                                                              To learn more, see our tips on writing great answers.




                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function () {
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263825%2fadd-css-to-a-javascript-button%23new-answer', 'question_page');
                                                              }
                                                              );

                                                              Post as a guest















                                                              Required, but never shown





















































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown

































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown







                                                              Popular posts from this blog

                                                              List item for chat from Array inside array React Native

                                                              Thiostrepton

                                                              Caerphilly