0

My javascript code something like this

if (editMode === true) {
    var divId = element.attr('id');
    console.log(divId);
}

var textBox = $('<input class="my-text-box" onchange="myFunction(this.value)" />');

I want to set divId variable as the ID in this...

var textBox = $('<input class="my-text-box" onchange="myFunction(this.value)" />');

this divId variable is with dynamic values...

please help me to do that.

Ján Kyselica
  • 701
  • 5
  • 15

3 Answers3

0
var txtBox = $('.my-text-box');
txtBox.prop('id', 'newIdHere');

don't try to select an element by the entire element html like you were , ex.

var textBox = $('<input class="my-text-box" onchange="myFunction(this.value)" />');
Scott Selby
  • 9,420
  • 12
  • 57
  • 96
0
var divId = null;
if (editMode === true) {
    divId = element.attr('id');
    console.log(divId);
}

$('.my-text-box').attr('id', divId);
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

You need to just concat that id with html

if (editMode === true) {
var divId = element.attr('id');
console.log(divId);}

var textBox = $( '<input class="my-text-box" onchange="myFunction(this.value)" id="'+divId+'" />');
Shailendra Sharma
  • 6,976
  • 2
  • 28
  • 48