0

Hello i have a td in table with value i have changed this td to an input of type text when a click to a button.

And now i want to change the id of the new input with the id of the td element so i have an exception that is :

input.id is not a function

Here my code js :

$("tr.forc td.TdbeforeForcage").each(function () {
    var html = $(this).html();
    var input = $('<input class="numberforce" style="width:50%" type="text" />');
    var IdTd = $(this).attr("id");
    input.val(html);
    input.id(IdTd); // Here trying to change id of input with id of td 
    $(this).html(input);

    if (html.indexOf('&nbsp;') > -1) {
      var newValue = html.replace('&nbsp;', ' ');
      $(this).find('input.numberforce').val(newValue)
    }
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
mecabmecab95
  • 101
  • 1
  • 3
  • 10
  • All these answers and no one using best practice of `prop()`. Also note @OP that you appear to be creating a duplicate `id` (as you're taking it from the `.TdbeforeForcage` and applying the same value to the new `input`. That's invalid. In fact, I'd suggest that creating `id` attributes dynamically is a rather large code smell. – Rory McCrossan Feb 07 '19 at 13:52

4 Answers4

5

Because it is not a function it's a property, you would change it like so:

input.id = IdTd;

when using jQuery:

input.attr('id', IdTd);
Marcin
  • 1,488
  • 1
  • 13
  • 27
3

Use:

$(input).attr("id", "newID");

Nicolae Maties
  • 2,476
  • 1
  • 16
  • 26
0

Id is an attribute no a function. To set an id you can set it like this:

input.id = "id"; // input is an Element object

or

input.attr("id", "newId"); // input is a jQuery object
hawks
  • 861
  • 1
  • 8
  • 20
0

Check the jquery attr method here http://api.jquery.com/attr/#attr2

input.attr('id', IdTd);
Stavros
  • 655
  • 7
  • 16