user3319384
user3319384

Reputation: 33

To get value of textbox

In the below code I have a textbox and I am binding the textbox to a datalist. So now there are four textboxes. My actual aim is to get the values of all 4 textboxes but I can get only one value of one textbox. Please help me to solve the issue. Js:

function check(){
    var value = document.getElementById("<%= txtField.ClientID %>").value;
    alert(value);//
}

asp.net:

<asp:TextBox ID="txtField" runat="server" width="200Px"></asp:TextBox>

Upvotes: 1

Views: 72

Answers (1)

Mayank Pathak
Mayank Pathak

Reputation: 3681

ClientID will fire only for a Particular Textbox of First match found only. You should have written some Jquery for it. Try it

str = "";
$('input[type=text]').each(function (){
   str+=$(this).val() + "$";
  });
if(str != "")
  str = str.substring(0,str.length-1);

alert(str);

FIDDLE

Explanation :

input[type=text] selector will work for every input control of DOM which is of Text type. .each function of jQuery will iterate through all the textboxes of DOM and concates in str . And after completion of iteration its showing all concatenated values using alert $(this).val() will pull out values of all textboxes found in that context.

Upvotes: 2

Related Questions