Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically change the jQueryUI icon of a button after initialization?

<script>
    $(function() 
    {
        $( "#dynabutton" ).button(
        {
            icons: 
            {
                primary: "ui-icon-gear"
            },
            text: false
        });

        $( "#swap" ).button(
        {
            icons: 
            {
                primary: "ui-icon-locked"
            },
            text: true
        }).click(function(event)
        {
            // change #dynabutton icon from
            // "ui-icon-gear"
            // to:
            // "ui-icon-locked"
        });         
    });
    </script>



<div class="demo">

<button id="dynamic_button">Button with gear icon</button>
<button id="swap">Swap icons</button>

</div>

On click of the #swap button, I want to switch the icon (jQueryUI icon) associated with #dynabutton from ui-icon-gear to ui-icon-locked.

But I don't know if this is supported?

like image 458
Michael Murphy Avatar asked Dec 15 '10 15:12

Michael Murphy


1 Answers

You can call .button("option", options) to set options later (like other jQuery UI widgets), including the icons:

$(function() {
    $( "#dynabutton" ).button({
        icons: { primary: "ui-icon-gear" },
        text: false
    });
    $( "#swap" ).button({
        icons: { primary: "ui-icon-locked" },
        text: true
    }).click(function() {
        $( "#dynabutton" ).button("option", {
          icons: { primary: "ui-icon-locked" }
        });
    });         
});

You can test it here.

like image 102
Nick Craver Avatar answered Nov 16 '22 02:11

Nick Craver