Azeem Raza Tayyab
Azeem Raza Tayyab

Reputation: 63

How to set the value in the textboxes - asp.net using jquery?

I have a table with id 'tbl' which contains textbox controls with id's like below

<table id="tbl">

txt1Text1
txt1Text2

txt2Text1
txt2Text2

txt3Text1
txt3Text2
.................
.................

I want to set the specif value in the textboxes that have id ends with Text1 I want to do it using jquery/javascript.

Thanks for help.

Upvotes: 3

Views: 1892

Answers (5)

Steve B
Steve B

Reputation: 37660

You should add a fake css class, that allows you to "tag" the textboxes, then use jQuery to find these textbox using the css class selector.

<asp:TextBox runat="Server" CssClass="existingClass FakeClass" id="txt1" />
<asp:TextBox runat="Server" CssClass="existingClass FakeClass" id="txt2" />
<asp:TextBox runat="Server" CssClass="existingClass FakeClass" id="txt3" />

<script type="text/javascript">
    $(function(){
        $(".FakeClass").val("42");
    });
</script>

What is important here, is that the "FakeClass" does not have to exists. It's only a marker.

Upvotes: 1

Gyan Chandra Srivastava
Gyan Chandra Srivastava

Reputation: 1380

$('input[type=text][id$=Text1]').val('value');

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68400

Try with this

$('#tbl input[id$="Text1"]').val('my value');

Attribute Ends With Selector

Selects elements that have the specified attribute with a value ending exactly with a given string. The comparison is case sensitive.

Upvotes: 0

user659025
user659025

Reputation:

$("input[id $= Text1]").val('your value');

Upvotes: 0

Ram
Ram

Reputation: 144669

You can use Attribute Ends With selector.

$('#tbl input[type=text][id$=Text1]').val('new value')

Upvotes: 5

Related Questions