0

Is it possible to change buttons based on the text in a td cell with jquery? I don't want to have two different buttons in a cell. Here's what it looks like right now in jsfiddle. (Sorry about the lack of jquery, I don't know how to approach it)

I have three stages in my mysql table. The column process goes from waiting to processing to shipped.

When it is process and waiting, btn_cancel is displayed.

<input type="submi" class="btn btn-danger" id="btn_cancel" name="cancel" value="Cancel Order">

When it is shipped, btn_hidden is displayed.

<input type="submit" class="btn btn-danger" id="btn_hidden" name="hidden" value="Hide">

How could I achieve this? Or if this is not possible what's the alternative?

6
  • is the value of the cell going to update dynamically? if not, why not give the button the correct value when the page is built in php? if it is dynamically, then whenever it is set, also change the button Commented Apr 3, 2015 at 19:15
  • look at conditional statement in php w3schools.com/php/php_if_else.asp. Make condition base on your status Commented Apr 3, 2015 at 19:17
  • @pala_ it is displayed in the table and an admin would change it so I would like it to change on the user side but not sure how to acheive this Commented Apr 3, 2015 at 19:27
  • well they'd have to refresh the page, in which case your server side scripts can generate the correct button Commented Apr 3, 2015 at 19:28
  • I have server side I just need to change the buttons. The data gets displayed on the page automatically with ajax @pala_ Commented Apr 3, 2015 at 19:43

2 Answers 2

1

The comments on your question may be correct but you asked how to do it in jQuery so here it is.

you can loop over each proccess table data and check the .text() and if the text === waiting then do what ever to the button. Here is a jsFiddle based off yours.

$(function(){

  $('[data-proccess]').each(function(ind, process){
    var content = $(process).text().toLocaleLowerCase();
    if(content !== 'waiting'){
      $(process).closest('tr').find('[name="cancel"]').hide();
    }  
  });

});

With that said. You might want to look in to doing it on the server side if you are generating the page with php or something

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried your solution and it works. However, it doesn't run the actions when I implement this. For instance, without the code, when I click the buttons to cancel and hide. They work but when I implement this and I clikc on the buttons, nothing happens.
what action are you referring to?
0

You might try the following. Please use appropriate value for #btn_id

$("table tr").each(function(i){
  var $tds = $(this).find("td");
  var status = $tds.eq(1).text().toLocaleLowerCase();

  if(status === 'waiting' || status === 'processing'){
    $tds.eq(2).find("#btn_id").attr('value', 'Cancel Order');
  }
  else if(status === 'shipped'){
    $tds.eq(2).find("#btn_id").attr('value', 'Hide');
  }
  else{
    $tds.eq(2).find("#btn_id").attr('value', 'Wait..');
  }
});

1 Comment

I have tried your solution and it works. However, it doesn't run the actions when I implement this. For instance, without the code, when I click the buttons to cancel and hide. They work but when I implement this and I clikc on the buttons, nothing happens.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.