Skip to content Skip to sidebar Skip to footer

Onclick Change List Styles

Let's say I have a simple list:
  • 1
  • 2
  • 3

    Solution 1:

    <!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="en"lang="en"><head><title></title><scriptlanguage="JavaScript"type="text/javascript">
                /*<![CDATA[*/
                var Lst;
    
                function CngClass(obj){
                 if (Lst) Lst.className='';
                 obj.className='Clicked';
                 Lst=obj;
                }
    
                /*]]>*/
            </script><style>.notClicked {color: black}
                .Clicked {color: red}
            </style></head><body><ul><li><aonclick="CngClass(this);"href="#"class="notClicked">1
                    </a></li><li><aonclick="CngClass(this);"href="#"class="notClicked">2
                    </a></li><li><aonclick="CngClass(this);"href="#"class="notClicked">3
                    </a></li></ul></body></html>

    Solution 2:

    Why change the style of the other? You may want to change the style of the clicked element.

    If so, you can use jQuery for that

    Example:

    <li class = "notClicked">element 1</li>
    <liclass = "notClicked">element 2</li><liclass = "notClicked">element 3</li>
    
    $('.notClicked').click(function()
    {
        $(this).addClass('active');
    });
    

    Solution 3:

    <script>functionchangeClass(){
            document.getElementById("idElement").setAttribute("class", "Clicked");
        }
    </script><ul><liclass="notClicked" >1</li><liclass="notClicked"onClick="changeClass()"id="idElement">2</li><liclass="notClicked">3</li></ul>

Post a Comment for "Onclick Change List Styles"