user1853803
user1853803

Reputation: 659

pick value from text box whose id generated dynamically

I am trying to pick the value from the text box.

Text box id data-id data-phase data-project is dynamically generated.

<input class="addinformation" type="text" data-id="" data-phase="1_2" data-project="1" value="" style="width: 50%;" id="IMxyz_addinfo">

My js code is

var TktNo ="IMxyz";

Method 1:

console.log($(".additionalInfoBox").find("[data-id='" + TktNo + "']").html());

Method 2

console.log($("#" + TktNo + "_addinfo").val());

Both are failing....any idea

console.log("1 " + $(".addinformation").val());
console.log("2 " + $(".addinformation").find("[data-id='" + TktNo + "']").val());
console.log("3 " + $("#" + TktNo + "_addinfo").val());
console.log("4 " + $('input#' + TktNo + '_addinfo').val());

output is

1 
2 undefined 
3  
4  

when i do console.log($(".addinformation")); following are the snap shot enter image description here enter image description here

Upvotes: 0

Views: 266

Answers (5)

Bharat Chodvadiya
Bharat Chodvadiya

Reputation: 1650

Use this.

console.log($("input.addinformation").val());

Or

var TktNo ="IMxyz";
console.log($("input#" + TktNo + "_addinfo").val());

Upvotes: 1

Saneem
Saneem

Reputation: 926

I've coded it on jsfiddle: http://jsfiddle.net/ZggHZ/. (Used alert just to test)

var TktNo ="IMxyz";

alert($('input.addinformation#'+TktNo+'_addinfo').val() 

);

Upvotes: 3

codeVerine
codeVerine

Reputation: 762

What are you doing here ? The class of your input field is addinformation and in your first method you are using some other class (additionalInfoBox) to select the element. It wont work. If there is only one input with the class addinformation , then this should work :

console.log($(".addinformation").val());

Upvotes: 1

Amjath Jayakumar
Amjath Jayakumar

Reputation: 51

console.log($("#" + TktNo + "_addinfo").val());

should return the value. make sure this is called after the html is rendered.

Upvotes: 1

Dale
Dale

Reputation: 10469

You could try this:

$('input#'+TktNo+'_addinfo');

Upvotes: 2

Related Questions