4

I am facing a very challenging problem due to my little knowledge in jQuery mobile. I have researched many solutions and implemented them but does not seem to work

  1. I have a button called click me
  2. When a user clicks on the button, the button becomes disabled and the text clickme changes to please wait...
  3. After 2 seconds the button become activated (no longer disabled) and the text changes from please wait... back to click me.
  4. I have gotten it to work to some extent. The code works pefectly with out this code line $(this).parent('').text('Please wait...'); but could one kindly advise how i get it to work with a please wait....

Any help or guidance will be much appreciated. My code is below:

<div class="ui-btn ui-input-btn ui-corner-all ui-shadow">
    clickme
    <input class="submit button-primary btn large send" id="wp-submit" name="up_submit" tabindex="250" type="button" value="clickme">
</div>
$(document).ready(function () {
    var clicked = false;

    $('#wp-submit').bind('click', function() {
        if(clicked == false) {
            $(this).button('disable');
            $(this).parent('').text('Please wait...');
            clicked = true;

            setTimeout(function() { 
                $('#wp-submit').button('enable'); 
                $(this).parent('').text('click me');
                clicked = false;
            }, 2000); 
        }
    });       
});
ARTLoe
  • 1,743
  • 2
  • 21
  • 54
  • i got it to work as far as this, but the 'please wait...' line breaks the code `` – ARTLoe May 15 '15 at 08:27
  • ANSWER: http://stackoverflow.com/questions/30256665/changing-button-value-of-a-jquery-mobile-with-a-settimeout-function/30260378#30260378 – ARTLoe May 16 '15 at 15:24

3 Answers3

2

setTimeout should be inside the event handler for the button click. You'll also have to use the button's selector, as you'll lose the context of this within the setTimeout callback, as well as set clicked back to false:

$(document).ready(function () {
    var clicked = false;

    $('#wp-submit').bind('click', function() {
        if(clicked === false) {
            $(this).button('disable');
            $('#wp-submit .ui-btn-text').val("Please wait...");
            clicked = true;

            setTimeout(function() { 
                $('#wp-submit').button('enable'); // 'this' no longer refers to button element
                $('#wp-submit .ui-btn-text').val("clickme");
                clicked = false;
            }, 2000); 
        }
    });       
});

Working fiddle: https://jsfiddle.net/7j0rfb56/

I've had to change up the code somewhat as core jQuery does not provide a .button function (perhaps this is jQuery mobile specific).


EDIT: I've improved my previous answer based on your updated code:

There are a number of issues here, I'll try to explain each as best I can:

  • Firstly your provided HTML includes the text in two places - is this intentional? The value="clickme" attribute on the button will set the button text. You may or may not wish to ommit the extra text immediately within the div tag. I've assumed you want to change both in the example.
  • Changing the text of an element with .text() also replaces the entire contents of that element. So when calling $(this).parent('').text('Please wait...'); you are replacing both the text and the button element with the new value. I recommend wrapping the text you wish to change in a label tag so you can call .text() on that instead.
  • As mentioned already, you are unable to access the button via $(this) within the setTimeout callback as the context changes. You'll have to store a reference to it beforehand that you can use in the callback.
<div class="ui-btn ui-input-btn ui-corner-all ui-shadow">
    <label>clickme</label>
    <input class="submit button-primary btn large send" id="wp-submit" name="up_submit" tabindex="250" type="button" value="clickme" />
</div>
$(document).ready(function () {
    var clicked = false;

    $('#wp-submit').bind('click', function() {
        if(clicked == false) {
            // store reference to the button clicked
            // this allows us to access it within the 
            // setTimeout callback below.
            var $btn = $(this);

            $btn.button('disable');
            $btn.siblings('label').text('Please wait...'); // sets the text for the label only
            $btn.val('Please wait...'); // sets the text on the button itself
            clicked = true;

            setTimeout(function() {
                // we can access our '$btn' variable inside
                // here, this avoids the issue of 'this'
                // losing context
                $btn.button('enable'); 
                $btn.siblings('label').text('click me');
                $btn.val('click me');
                clicked = false;
            }, 2000); 
        }
    });
});

Sample jsfiddle: https://jsfiddle.net/vsdv9ghL/ (again, I've removed any non jQuery-mobile code).

rmorrin
  • 2,305
  • 1
  • 15
  • 18
  • thanks @rmorrin, your code worked, the only issue is that is did not change the button from `clickme` to `please wait...` so i am currently merging it with @kalmani code to see if it will work – ARTLoe May 11 '15 at 10:03
  • The selector: `$('#wp-submit .ui-btn-text').val("Please wait...")` assumes there is some child-element of the button with the `.ui-btn-text` class. Is this the case? I couldn't see any such element in your provided markup. Perhaps you mean to use `$('#wp-submit').val('Please wait...')` instead? – rmorrin May 11 '15 at 10:33
  • thank you @rmorrin for your help. i have updated the html code within the question as displayed by jquery mobile. i have partially gotten the code to work but it breaks on this line `$(this).parent('').text('Please wait...');`. I believe i have not grabbed the html code content correctly. any help would be much appreciated – ARTLoe May 15 '15 at 08:46
  • @ARTLoe That's a huge help, thanks! There are a few issues with what you've posted, I've updated my original answer to account for the new code. Hope this helps! – rmorrin May 15 '15 at 10:06
  • thank you @rmorrin, your answer is very helpful, but i am specifically looking for the solution for jquery mobile as jquery on its own does not solve it. For example, the html content i pasted is the exact way Jquery mobile displays it without `label` - i am unable to place `label` where you have suggested as this is by jquery mobile - i tried modifying your code accordingly but still no luck... the code works, it's just the value `click me` does not change... – ARTLoe May 15 '15 at 10:29
  • 1
    @ARTLoe Perhaps [this answer](http://stackoverflow.com/q/5919001/1794631) will help. I've only taken a brief look, but that'd mean using `$btn.prev('a').find('span.ui-btn-text').text('Please wait...');` in your example as it looks like jquery mobile renders some additional markup for buttons and label text. – rmorrin May 15 '15 at 10:39
  • i came across that solution, my jquery mobile code produced did not have a span so i coded it like this `$btn.prev('a').find('.ui-btn-text').text('Please wait...');` but still not working. i know i am almost there - its just figuring how to grab the jquery mobile html content `click me`. i tried this too `var replaced = $(this).parent().html().replace("clickme", "please wait..."); $(this).parent().html(replaced);` but no luck... – ARTLoe May 15 '15 at 10:54
  • sorry, i meant edited it accordingly like this `$btn.prev('div').find('div.ui-input-btn').text('Please wait...');` but no luck... – ARTLoe May 15 '15 at 11:14
0

no need to use jQuery for those little dom manipulation :

$(document).ready(function () {
  var clicked = false,
      button = null;

  $('#wp-submit').bind('click', function() {
    button = this; // storage your button here
      if(clicked === false) {
          button.disabled = true;
          button.value = "Please wait...";
          clicked = true;
          console.log('disabled');
        setTimeout(function() { // link your timeout with the click event
          console.log('enabled');
          clicked = false;
          button.disabled = false;
          button.value = "clickme";
        }, 2000);
      } 
  });

});

Kalmani
  • 51
  • 3
  • thank you @kalmani for your help. i have updated the html code within the question as displayed by jquery mobile. i have partially gotten the code to work but it breaks on this line `$(this).parent('').text('Please wait...');`. I believe i have not grabbed the html code content correctly. any help would be much appreciated – ARTLoe May 15 '15 at 08:44
0

many thanks to @ezanker who solved this question here --> Changing button value of a jQuery mobile with a setTimeout function

Answer:

$(document).ready(function () {

    var clicked = false;
    $('#wp-submit').on('click', function() {
        if(clicked == false) {
            var $btn = $(this);
            $btn.button('disable').val('Please wait...').button('refresh');
            clicked = true;

            setTimeout(function() { 
                $btn.button('enable').val('click me').button('refresh');
                clicked = false;
            }, 2000); 
        }
    });
});
Community
  • 1
  • 1
ARTLoe
  • 1,743
  • 2
  • 21
  • 54