How to change a JQuery Mobile button icon with javascript

后端 未结 3 740
谎友^
谎友^ 2021-01-14 20:43

I would like to have a button that changes data-icon class depending upon user selections

Example button would be:



        
相关标签:
3条回答
  • 2021-01-14 21:06

    You can use $('selector').attr('data-icon', 'newValue') to change the attribute.

    The documentation

    0 讨论(0)
  • 2021-01-14 21:08

    jQuery Mobile has a predefined function:

    $('a').buttonMarkup({ icon: "star" });
    

    It is not enough to change an attribute, final button restyling must be done with .buttonMarkup( function.

    Here's an official documentation: http://jquerymobile.com/demos/1.2.0/docs/buttons/buttons-options.html

    And here's an example: http://jsfiddle.net/Gajotres/AmFKa/

    Also, because you are creating a custom button it wont be enough just to change icon name, first you need to define it in your css like this:

    <style type="text/css">
        .ui-icon-edit {
           background-image: url(icons-18-white.png); // This should be your picture
           background-repeat: no-repeat;
           background-position: -824px 50%;
        }
    </style>
    

    We would add this new icon like this:

    $('a').buttonMarkup({ icon: "edit" });
    

    In this case we are adding picture edit. Now notice its cass name .ui-icon-edit, to add a new icon class you need to combine a .ui-icon- string with a icon name. In your case your class name would be .ui-icon-another-flag.

    0 讨论(0)
  • 2021-01-14 21:08

    Just pass in the value to the data function. See the documentation

    $("#language2").data("icon", "another-flag");
    
    0 讨论(0)
提交回复
热议问题