Țaman ŞheƦzad Țaman ŞheƦzad - 4 months ago 59
HTML Question

jQuery/JavaScript toggle button to add Inline styles

How to create a toggle button to add inline styles?

For example in this simple structure i want to make a toggle button to show or hide the div
I do not want to add a class name or using only .show() and .hide() in jQuery

I need it to be Inline style for example If i toggle the button i want it to add this style

style="display:block color:red;"


and if you toggle again

style="display:none"




<button>toggle</button>

<div>blah bla blah</div>




Answer

You can check toggle style with .css("display") .Then set attribute as you wanted to do

$("button").click(function(){
        if($("div").css("display") == "none") {
             $("div").css({"display":"block","color":"red"});
        }
        else{
            $('div').css("display","none");
        }
    });
Comments