0

For some reason I need to convert jquery variable value into object.

$("input[type=text]").on("keyup", function () {
    var target = this.id; //outputs 'subject'
    //how to convert here?
    var targetItem = $("#subject");//originally like this.
    //I tried something like below
    //var targetItem = $("#+target+");
    alert(targetItem);
});

All I'm trying to do is , changing the word in this: $("#subject") with whatever value of variable 'target'.

Nikunj Chotaliya
  • 802
  • 8
  • 19
112233
  • 2,406
  • 3
  • 38
  • 88

2 Answers2

2

Use this -

var targetItem = $(this); // Will always hold the current element object

And for the code you are using it should be -

var targetItem = $("#" + target);

target is a variable. "#+target+" will be treated as #+target+ not #subject (in your case)

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
1

Simply like

var target = this.id;
var targetItem = $("#"+target);
Sudharsan S
  • 15,336
  • 3
  • 31
  • 49