0

I'm having problems changing the text of a button control.

This is the html code of my button

<input type="submit"  name="submit" id="btnsubmit" class="btn btn-default btnsubmit" value="Save" />

I want it to be done after populating the html table via php

This is my jQuery code

$( document ).ready(function() {
    var checkedItems = $('#dataTable tr td').each(function() {
});
if (!checkedItems.size()) {
    alert ("No data on the table");
}else  {
alert ("this is else");
    document.getElementById('btnsubmit').innerHTML = "Update";
}
});

What is wrong with my code?

I'm also using document.getElementById('btnsubmit').innerHTML = "Update"; to change the text of an input button in other page, why doesn't it work here?

The alert that shows is:

this is else

Johnie Karr
  • 2,744
  • 2
  • 35
  • 44
knowmeifyou
  • 217
  • 7
  • 17
  • How you are populating your table? via ajax or postback? – Jai Dec 08 '15 at 13:39
  • via php sir. i even put the javascript code below the code for populating the table to make sure it work. but it didn;t – knowmeifyou Dec 08 '15 at 13:41
  • You need to wire your code up to whatever event is expected to trigger this. Sounds like you might want to look at $.ajaxSuccess – garryp Dec 08 '15 at 14:33

3 Answers3

1

For this you can use a flag:

$( document ).ready(function() {
    var checkedItems = false; // check with flag;
    $('#dataTable tr').each(function() {
        $('td', this).text().trim() === "" ? checkedItems = false : checkedItems = true;
    });
    if (checkedItems) {
        alert ("No data on the table");
    } else  {
        alert ("this is else");
        $('#btnsubmit').val("Update");
    }

});
Jai
  • 74,255
  • 12
  • 74
  • 103
0

It depends on how you populate that table. Are you using, for example $.ajax method or anything similar? Because you could actually use a done promise over there, and in that action use a jquery selector to find a button and change txt of a button by simple (for example)

$('#submit-button').val('Update')
Shiroo
  • 666
  • 4
  • 11
0

I believe you want see the answer to this question for updating button text. However unless you are using a combination of AJAX and PHP, you may consider just using PHP to set your button text rather than relying on Javascript. Since your table is populated by PHP, the button text can be determined using PHP before it even reaches the client. This way, you can reduce page load by removing unnecessary script.

Community
  • 1
  • 1