Reputation: 4126
I have hard time to find textbox values in my repeater control. On button click i need to get textbox value corresponding to textbox which are inside of button which is clicked.
So i have :
Textbox1 ---> which have to be bound with Button1 TextBox2 ----> which is bound with Button2
<span>Question :</span><span>my second content <br /></span><span>Answer : </span><input name="ctl00$MainContent$ctl00$ctl00$TextArea" type="text" id="MainContent_ctl00_TextArea_0" value="6454" />
Add Question
Question :abcdedf
Answer : Add Question
Right now i have jquery function which get which is clicked:
$(".addAnswerButton").click(function () {
//var quantityBox = $("#TextArea", $(this).parent());
var tr = $(this).closest("text")
alert(tr.val());
//quantityBox.text = "Hello there";
//var currentValue = quantityBox.val();
$(this).siblings('input[type="text"]').first().val("clicked");
//quantityBox.val(parseInt(currentValue) + 1);
});
Ive got it working partially with:
$(this).siblings('input[type="text"]').first().val("clicked");
Where i get set textbox text, but then again, if i click second button, nothing happends.
What an i doing wrong? How can i get value from specific textbox?
UPDATE
now i go with solution where i add custom attribute to each control:
<span>Question :</span>
<span>my second content <br /></span>
<span>Answer : </span>
<input name="ctl00$MainContent$ctl00$ctl00$TextArea" type="text" id="MainContent_ctl00_TextArea_0" value="654" mydataid="2" />
<button id="MainContent_ctl00_answerQuestion_0" Class="addAnswerButton" onclick="Click()" mydataid="2" >Add Question</button><hr /><span>Question :</span><span>abcdedf <br /></span>
<span>Answer : </span><input name="ctl00$MainContent$ctl00$ctl01$TextArea" type="text" mydataid="1" id="MainContent_ctl00_TextArea_1" value="654" />
<button id="MainContent_ctl00_answerQuestion_1" Class="addAnswerButton" onclick="Click()" mydataid="1">Add Question</button>
So right now i can get custom atribute from onclick event by doing this:
$(".addAnswerButton").click(function() {
var pos = $(this).attr("mydataid");
alert(pos);
// $(this).siblings('input[type="text"]').first().val("clicked");
});
But how do i get textbox control which have samme custom attribute name as button?
Update 2
After 70 tryes i finally figured it out (my first tryes with Jquery)
solution
$(".addAnswerButton").click(function() {
var pos = $(this).attr("mydataid");
results = $('input[type = "text"][mydataid="'+pos+'"]').first();
alert(results.val());
});
So point is, you are getting custom attributes "name", and then searching for a textbox with same attribute name.
Upvotes: 2
Views: 7613
Reputation: 1360
Edited after the comments.
I head to your jsfiddle and make it work with this:
$('[id*="answerQuestion"]').click(function () {
$(this).parent().children('[id*="TextArea_' + $(this).attr('id').substring($(this).attr('id').length - 1) + '"]').val('clicked');
});
Explanation:
That's it! It's a little tricky but it's the best solution i found.
Hope this helps!
Greetings, sorry if there are some english inconsistencies :)!
Upvotes: 1
Reputation: 1955
You can mark them from server with some attribute and use it in your jQuery code.
The output from server would be like this:
<input type='text' data-item-id='1'>First</input>
<inpyt type='button' data-item-id='1'></input>
<input type='text' data-item-id='2'>Second</input>
<inpyt type='button' data-item-id='2'></input>
After that once button is pressed, get it's data-item-id and search for textbox with the same attribute value.
Upvotes: 2