3

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.

5
  • please specify whether the control is a html control or server control Commented Sep 25, 2012 at 12:51
  • Please note one point, he want to set the value of only those controls, that have id ends with 'Text1'. In question, there are 3 controls. Commented Sep 25, 2012 at 12:55
  • 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. In fact, you should also descrive how you create the textboxes. One by one? using a databound control? using mvc? created on the fly? With the answer to this question, you will get far more better answers as there are probably thousands way to solve the issue. Commented Sep 25, 2012 at 12:56
  • @SteveB, post that answer and I'll upvote it. That seems to be the "best practice" answer. Commented Sep 25, 2012 at 12:58
  • @Neil: I will when azeemraza will give more details on how he creates the textboxes. Commented Sep 25, 2012 at 13:01

5 Answers 5

5

You can use Attribute Ends With selector.

$('#tbl input[type=text][id$=Text1]').val('new value')
Sign up to request clarification or add additional context in comments.

1 Comment

+1. Only answer which distinguishes a textbox from an input tag.
1

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.

Comments

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

Comments

0

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.