2

I have a Repeater (ASP.Net).

<asp:Repeater ID="rep" runat="server">
<ItemTemplate>
<asp:TextBox runat="server" ID="txt"></asp:TextBox>
<asp:CheckBox runat="server" ID="cb" />
</ItemTemplate>

I would like to write Text in the text-box if I click on the checkbox, using jQuery. Is this possible and how.

4 Answers 4

4

I'd recommend simply using a CSS class and using that to identify the TextBox:

<asp:Repeater ID="rep" runat="server">
  <ItemTemplate>
    <asp:TextBox runat="server" ID="txt"></asp:TextBox>
    <asp:CheckBox runat="server" ID="cb" CssClass="repCheck" />
  </ItemTemplate>

And then:

$('.repCheck').change(function(){
    // via Andre
    $(this).siblings('input[type="text"]').first().val("clicked");
});
Sign up to request clarification or add additional context in comments.

4 Comments

what if i am having multiple text boxes... and i want to change values of each?
@RonakBhatt That sounds like a separate question :)
@RonakBhatt - Comments aren't intended for converstaion, but you can simply access multiple inputs from within the checkbox change handler. If they are different, you can narrow the selector using a class.
thanx @rechard.... Actully i was assuming the same output.. but was not confident over it..
1

You'll have to post the html output to get a working code, but it is something like this:

$(function(){
    $('#<%=rep.ClientID  %> input[type="checkbox"]').each(function(){
        $(this).change(function(){
            $(this).siblings('input[type="text"]').first().val("clicked");
        });
    });
});

Comments

0

Using jQuery, you can use the ID selector to select the DOM elements with the ID="txt" that you have specified. http://api.jquery.com/id-selector/

You can then update the .text() attribute of those elements. http://api.jquery.com/text/

Something like this should work in the simplest fashion:

$("#txt").text("some new text");

1 Comment

In a Repeater, the actual id property will have an autogenerated value.
0

Yes, it is. One possibility is the approach by the server:

On the Load Event (in the server) of the CheckBox, you can do the following:

void checkBox_Load(object sender, EventArgs e)
{
     CheckBox chk = (CheckBox) sender;
     TextBox txt = (TextBox) chk.Parent.FindControl("txt");
     chk.Attributes.Add("onclick",
                          string.Format("$(\"#{0}\").val('{1}');", txt.ClientID, "newValue");
}

EDIT: It was missing the # in the jQuery selector.

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.