1

I am using jaquery and jquery mobile version 1.8 and I have a button like so:

   <div class="ui-bar-a" id="myButton" style="bottom:0;position: fixed;width: 100%">
        <input type="submit" name="Next" id="NextButton" value="Save"  /> 
   </div>

And I have a javascript that can change the text for it like so:

     $('#AnyButton').live('click', function() {             
        if(true)
        {
            $('#myButton div').text('Saving')
        }
        else
            $('#myButton div').text('Continue');
    });

I tried so many other ways that didn't work but this works however after I change the text the button seems to replace the myButton div content with the text Saving or Continue and thus the button is no longer clickable.

In my browser debugger the button shows a text Save appear between myButton and the Nextbutton input.

Like so:

<div class="ui-bar-a" id="myButton" style="bottom:0;position: fixed;width: 100%">
"Save"
      <input type="submit" name="Next" id="NextButton" value="Save"  /> 
</div>
7
  • 2
    "seems to replace the myButton div content" $('#myButton div').text('Saving') , well yeah you instructed it to do so. If you want to change the value of the input then you should target the input $('#NextButton').val('Saving').
    – Uroš
    Commented Jul 10, 2020 at 13:49
  • @Uroš I tried that first thing and it didn't work for me. I actually tried many ways with props etc .. none replaced the text except this way. Commented Jul 10, 2020 at 13:51
  • are you interested only to change the string "Save" in the div?
    – gaetanoM
    Commented Jul 10, 2020 at 14:06
  • @gaetanoM not really but nothing else seem to be working so How is that possible? Commented Jul 10, 2020 at 14:09
  • .live( has been removed use .on('click', function AND a newer version as 1.8 has security vulnerabilities and newer will be faster Commented Jul 10, 2020 at 14:26

2 Answers 2

2

I suspect there is more to your code than you have presented. With jQuery Mobile, each input is read as a Button. So you mnay need to refresh it after a dynamic update.

This code is working:

$(function() {
  $("#NextButton").click(function(e) {
    e.preventDefault();
    $(this).val("Saving").button("refresh");
  });
});
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<div class="ui-bar-a" id="myButton" style="bottom:0;position: fixed;width: 100%">
  <input type="submit" name="Next" id="NextButton" value="Save" />
</div>

See More: https://api.jquerymobile.com/button/ and https://demos.jquerymobile.com/1.4.5/button/

5
  • I am not using that version of JQuery and it is not using this click button event. it is a different event that needs to trigger it. I am using 1.8 as in the question. Commented Jul 10, 2020 at 14:17
  • @NoviceDeveloper please update your post with a Minimal, Reproducible Example: stackoverflow.com/help/minimal-reproducible-example Your OP does not specify that you are using jQuery 1.8
    – Twisty
    Commented Jul 10, 2020 at 14:18
  • @RyanWilson am not seeing an error in FireFox. Please clarify.
    – Twisty
    Commented Jul 10, 2020 at 14:26
  • @RyanWilson fixed. the Snippet was using HTTP and Chrome is sensitive to non-secure links. Switched to HTTPS and it should work for you now in the Snippet.
    – Twisty
    Commented Jul 10, 2020 at 14:36
  • Refresh did it : ) .button("refresh"); Commented Jul 10, 2020 at 18:10
1

If I am reading your post correctly, you want to change the text of the input, you need to change it's value property. I think .live is relatively old and deprecated and should be replaced with .on and a delegated event handler

$('#AnyButton').live('click', function() {             
    if(true)
        $('#myButton').find('input[type="submit"]').val('Saving');
    else
        $('#myButton').find('input[type="submit"]').val('Continue');
});
5
  • this does change the value just like '$('#NextButton').val('Saving')'. however, the problem is the text on the button does not change. I checked the value of the input using an alert that shows correctly the text again on button does not update. Commented Jul 10, 2020 at 14:01
  • @NoviceDeveloper Do you have multiple buttons in your Html with the same id? Is your button a part of a form, and causing a postback on click? We really need more information as to what you are currently doing in your code and your Html. Specifically, what is #AnyButton? Commented Jul 10, 2020 at 14:20
  • It is part of a form post back. anybutton is just any random button, it does trigger the action. Commented Jul 10, 2020 at 15:52
  • If you know in the form post anything else is happening then that maybe the answer otherwise this way also works: jsfiddle.net/q8xwLjuy Commented Jul 10, 2020 at 17:30
  • @NoviceDeveloper If your button click is causing a postback, it will stop your javascript code from executing. Commented Jul 11, 2020 at 1:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.