knowmeifyou
knowmeifyou

Reputation: 217

how to call javascript code when html table value is populated

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

Upvotes: 0

Views: 952

Answers (3)

Jai
Jai

Reputation: 74738

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");
    }

});

Upvotes: 1

Jacob Venable
Jacob Venable

Reputation: 51

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.

Upvotes: 0

Shiroo
Shiroo

Reputation: 666

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')

Upvotes: 0

Related Questions