Search code examples
javascriptjqueryjquery-mobilecordova

Not able to change the dynamically generated button text in jquery mobile


I created button dynamically like in my phonegap application using jquery mobile.

var table = document.getElementById("skiTable");
var row = document.getElementById("skiparticulars");
var clone = table.rows[1].cloneNode(true);

var skiFile = clone.cells[0].getElementsByTagName('input')[0];
var skiformTitle = clone.cells[0].getElementsByTagName('input')[1];

skiFile.id = "skifile" + skiRowCount;
skiFile.value = "";
skiformTitle.id = "formTitle" + skiRowCount;

skiformTitle.value = "";
row.appendChild(clone);
skiRowCount++;

html code is like

<table>
 <tr>
   <td>
      <input type="hidden" id="skifile1" />
      <input type="button" id="formTitle1"/>
   </td>
 <tr>
</table>

and here is the code for change button value:

$("#skifile" + k).val(results.rows.item(j).ski_file);

        $('#formTitle'+ k).val(results.rows.item(j).formTitle);
        $("#formTitle"+k).button("refresh");
tried this also 
//$("#formTitle" + k).prop('value',results.rows.item(j).formTitle).button("refresh");

but it never change the value.

I refered this jquerymobile dynamically changing text for button issue

and this How to change the text of a button in jQuery?


Solution

  • jQuery Mobile 1.4 example:

    Working example: http://jsfiddle.net/Gajotres/8x8HE/

    JavaScript

    $('#formTitle1').parent().html($('#formTitle1').parent().html() + 'Button text');
    

    Button HTML without text:

    <div class="ui-btn ui-input-btn ui-corner-all ui-shadow">
        <input type="button" id="formTitle1">
    </div>
    

    Button HTML with text

    <div class="ui-btn ui-input-btn ui-corner-all ui-shadow">
        <input type="button" id="formTitle1">Button text
    </div>
    

    Read this article if you want to learn how to customize jQuery Mobile elements.

    jQuery Mobile 1.3 and below example:

    Working example: http://jsfiddle.net/Gajotres/wLzA7/

    JavaScript

    $('#formTitle1').parent().find('span span').html('Button text');
    

    Button HTML without text:

    <div data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-theme="c" data-disabled="false" class="ui-btn ui-shadow ui-btn-corner-all ui-btn-up-c" aria-disabled="false">
        <span class="ui-btn-inner">
            <span class="ui-btn-text"></span>
        </span>
        <input type="button" id="formTitle1" class="ui-btn-hidden" data-disabled="false"/>
    </div>
    

    Button HTML with text

    <div data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-theme="c" data-disabled="false" class="ui-btn ui-shadow ui-btn-corner-all ui-btn-up-c" aria-disabled="false">
        <span class="ui-btn-inner">
            <span class="ui-btn-text">Button text</span>
        </span>
        <input type="button" id="formTitle1" class="ui-btn-hidden" data-disabled="false"/>
    </div>