Change button text in a table

Change button text in a table

mrsubbymrsubby Posts: 8Questions: 4Answers: 0

I have a situation that is similar to the following example https://datatables.net/examples/ajax/null_data_source.html

But when I click the button amongst other things I want to change the text the button shows. Specifically I want it to flip between Show and Hide. Seems simple enough, but I'm having real trouble just getting the text in the button to check which it is? How can this be done?

Note I suppose I could just have another array that flags the current value in the text (a boolean would do) and check that, but then I'd still need to be able to set the text in the button.

I could also use two images but I'd rather have buttons.

Thanks

Answers

  • mrsubbymrsubby Posts: 8Questions: 4Answers: 0

    I can easily tell is the button should say Show or Hide based on the row.child.isShown property, but how do I change the text on the button for just this instance?

  • mrsubbymrsubby Posts: 8Questions: 4Answers: 0

    Solved...

    Inside the if(row.child.isShown) adjust the innerHtml of the button and similarly in the else e.g. using the above linked example

    $('#example tbody').on( 'click', 'button', function () {
        var data = table.row( $(this).parents('tr') ).data();
        alert( data[0] +"'s salary is: "+ data[ 5 ] );
    
        if (row.child.isShown()) {
            this.innerHTML = "Show";
        }
        else {
            this.innerHTML = "Hide";
        }
    } );
    
This discussion has been closed.