Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable a link button in jQuery Mobile?

I have a jQuery Mobile Beta 1 website with jQuery 1.6.1 link button like this:

<a id="subselection" href="#" data-role="button" data-theme="b">TEST</a>

And in document.ready i set the click event:

$('#subselection').livequery("click", function(){
  alert('test');
});

I whant to disable this "link button" and i tried

$('#subselection').button('disable')

And that command set the button style like it is disabled but the click event works.

I have also tried

$('#subselection').prop('disabled', true);

and $('#subselection').prop('disabled'); gives true but its not disabled.

Someone has a good idea.

like image 854
RickardP Avatar asked Jun 22 '11 10:06

RickardP


3 Answers

Link button example:

  • http://jsfiddle.net/gRLYQ/6/

JS

var clicked = false;

$('#myButton').click(function() {
    if(clicked === false) {
        $(this).addClass('ui-disabled');
        clicked = true;
        alert('Button is now disabled');
    } 
});

$('#enableButton').click(function() {
    $('#myButton').removeClass('ui-disabled');
    clicked = false; 
});

HTML

<div data-role="page" id="home">
    <div data-role="content">

        <a href="#" data-role="button" id="myButton">Click button</a>
        <a href="#" data-role="button" id="enableButton">Enable button</a>

    </div>
</div>

NOTE: - http://jquerymobile.com/demos/1.0rc2/docs/buttons/buttons-types.html

Links styled like buttons have all the same visual options as true form-based buttons below, but there are a few important differences. Link-based buttons aren't part of the button plugin and only just use the underlying buttonMarkup plugin to generate the button styles so the form button methods (enable, disable, refresh) aren't supported. If you need to disable a link-based button (or any element), it's possible to apply the disabled class ui-disabled yourself with JavaScript to achieve the same effect.

like image 98
Phill Pafford Avatar answered Sep 24 '22 01:09

Phill Pafford


The a element does not have a property disabled. So defining one won't affect any event handlers you may have attached to it.

example: http://jsfiddle.net/niklasvh/n2eYS/

For a list of available attributes, have a look at the HTML 5 reference.

To solve your problem, you could instead for example assign the disabled as data in the element:

$('#subselection').data('disabled',true);

and then in your event check if its set:

if (!$(this).data('disabled'))

example: http://jsfiddle.net/niklasvh/n2eYS/5/

like image 43
Niklas Avatar answered Sep 23 '22 01:09

Niklas


No need for jquery/javascript in this case. Simply add a 'ui-disabled' class.

Like for example:

<a class="ui-disabled" id="subselection" href="#" data-role="button" data-theme="b">TEST</a>

Thats what I do, and it works for me ;)

like image 26
Gilly Avatar answered Sep 20 '22 01:09

Gilly