Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery mobile data-icon

Hi I am having difficulty changing some attributes in jquery mobile dynamically for some reason. I can see that the attributes are being changed in the markup when I call the respective methods I am using but the appearance of the elements does not change. Is there a need to reinitialize a lists etc in Jquery Mobile?

By the way here is some code to show you how I am setting the attributes:

$('.className').each(function(){
    if ($(this).text() == tempLoc){
    console.log('FOUND MATCH WITH tempLoc and ' + $(this).text());
    $(this).attr('data-icon','alert');
    $(this).attr('data-theme','e');
}
});

As I said this is working in code but the elements don't update or change appearance-wise. Any ideas?

UPDATE: I have found a way to update the data-icon but it is of course still messy and unnecessarily so. You can access the data-icon using the following method:

$(this).children('div.ui-btn-inner').children('span.ui-icon').removeClass('ui-icon-arrow-r');
$(this).children('div.ui-btn-inner').children('span.ui-icon').addClass('ui-icon-alert');

In that case $(this) refers to the button itself and the icon itself is found in it's ui-btn-inner child's span. ui-icon-arrow-r will result. Substitute whatever classes you are using in this case. If anybody knows of a way to refresh the buttons correctly I would appreciate it.

like image 701
Bob-ob Avatar asked Jul 19 '11 09:07

Bob-ob


People also ask

How to add an icon using jQuery?

In widgets where you set the icon with a data-icon attribute you use the name of the icon as value. For example: data-icon="arrow-r" . To add an icon to link buttons and button elements, use the name prefixed with ui-icon- as class. For example: ui-icon-arrow-r .

What are widgets in jQuery mobile?

A widget is a small gadget or control of your jQuery mobile application. Widgets can be very handy as they allow you to put your favorite applications on your home screen in order to quickly access them.

What is data-icon in HTML?

data-icon is a new alternative approach that uses the HTML5 data- attribute in combination with CSS attribute selectors. This new attribute lets us add our own metadata to elements, as long as its prefixed by data- and doesn't contain any uppercase letters.

What is page in jQuery mobile?

The page is the primary unit of interaction in jQuery Mobile and is used to group content into logical views that can be animated in and out of view with page transitions.


2 Answers

$("#myButtonName").buttonMarkup({ icon: "star" });

This will change it on the fly. Here is my code:

$(".menu-button").toggle(
    function()
    {            
        $(this).buttonMarkup({ icon: "star" });
        $(".navigation-menu-container").show();
    },
    function()
    {
       $(".navigation-menu-container").hide();    
    }
);

See docs here:

http://jquerymobile.com/test/docs/buttons/buttons-options.html

like image 127
user521990 Avatar answered Oct 04 '22 20:10

user521990


Related:

Live Example:

  • http://jsfiddle.net/phillpafford/8pwFK/29/ (The color and Icons change when you click them)

Link to Question that is related:

  • Specifying the styles for the selected tab in a navbar
like image 45
Phill Pafford Avatar answered Oct 04 '22 20:10

Phill Pafford