Jquery - Redeem the value of a 'td' and insert into a Text

Asked

Viewed 105 times

1

Good night ,

Next galley, I have a table from which I use the data from mysql , wanted to take the value of td ,and insert inside a TEXT that will appear inside the TD of the table with the value that was there so that it can be edited and updated in the database, I am trying to use Jquery to accomplish such feat but without success.

I tried something like this:

$(function(){
   $('#ID_DA_TD').click(function(){
      var teste = $('#ID_DA_TD').text();
      $('#ID_DA_TD').html('<input type="text" class="form-control form-control-sm" id="upgrade" style="width:150px;">');
      $('#upgrade').val(teste);
   });
});

Excuse my ignorance but I’m beginner, vlw.

  • Also put the html code of the table

1 answer

1


You don’t have to take it by the hair id (otherwise you will have to create several events for each id). Just click on the td. It is also necessary to create ids dynamics for the new inputs, otherwise there will be several inputs with the same id, what is wrong. You can create ids different using the index of td.

Also use the method .trim() to clear text ends.

Edit

Includes an auxiliary button to return the original state of the td:

$(function(){
   $('table td:not("input")').click(function(e){
      if($(this).find("input").length == 0){
         var idx = $('table td').index(this);
         $(this)
         .html('<input data-original="'+$(this).text().trim()+'" value="'+$(this).text().trim()+'" type="text" class="form-control form-control-sm" id="upgrade'+idx+'" style="width:150px;"><button>X</button>')
         .find('input').focus();
      }else if(e.target.tagName == "BUTTON"){
         $(this).html($(this).find("input").data("original"));
      }
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
   <tr>
      <td id="td1">
         texto 1
      </td>
      <td id="td2">
         texto 2
      </td>
   </tr>
</table>

  • Show , vlw brother

  • And to click and return to the original state to td ?

  • Where would be the click?

  • In the same td, for example click again and disappear the input text , and get the value again

  • Good morning , it worked, but if I click somewhere from the TD outside the text it takes the value of the button and inserts it into the text and updates it with the value.

  • Another thing if you can help me rescue the value of the input name ,because it would be something 'upgrade+index but I can’t rescue the index, the name actually

  • I got the thing to click on td. You’re putting "name+index" and want to rescue it?

  • For example, it will generate the name of this input with "upgrade + o index " but how can I redeem this value to send in the form

  • You see, the form will send the field normally with the name you have, no matter if it is "upgrade1" or "upgrade10"... what you have to know is how to capture the field after it is sent in PHP (if vc is using PHP)... this is because vc won’t know which name was sent, which could be any number in the index. That’s not it?

  • There in PHP, in the form capture, you need to somehow list all the variables sent by the form, so you can take the name that was sent, and then do what you want with it.

  • my form is generated within a foreach from an array which generates various inputs with the name = upgrade+idx+ if I receive them with the POST $_POST['upgrade'] if it is not the correct name will be null

  • I can do so then , foreach( $POST as key => value){ $value['name']?

  • Send these fields as an array. For example, put the name <input name="upgrade[]" /> and in PHP it will be an array that you can run and pick up the values.

  • In PHP you will get $_POST['upgrade'] which will be an array with all the values of the fields

  • so I don’t need to use idx anymore ?

  • In this case idx is unnecessary. I used idx only to generate different id for each input.

  • blz I’ll try here, thank you very much

Show 12 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.