Reputation: 2080
I have a code which is in PHP Loop.
<?php for($i = 1; $i++; $i < 10) { ?>
<div class="test">
<textarea name="test-textarea" for="<?php echo $i ?>"></textarea>
<button value="<?php echo $i ?>">Save</button>
</div>
<?php } ?>
Now I want to alert data in JQuery. When I click button which value is 7 then it will alert data of textarea which is for value is 7.
Any Help will be Appreciated.
Thanks.
Upvotes: 0
Views: 92
Reputation: 1457
Try this
<?php for($i = 1; $i < 10; $i++) { ?>
<div class="test">
<textarea id="<?php echo $i ?>" name="test-textarea" for="<?php echo $i ?>"></textarea>
<button onclick="javacript:alertvalue(<?php echo $i; ?>);" value="<?php echo $i ?>">Save</button>
</div>
<?php } ?>
<script type="text/javascript">
function alertvalue(var tid){
var txt=document.getElementById(tid).value();
alert(txt);
}
</script>
Upvotes: 0
Reputation: 9635
try this
$(document).ready(function(){
$(".test button").click(function(){
alert($(this).parent().find('textarea').val());
});
});
See Fiddle
Upvotes: 0
Reputation: 64526
You can include the value of the button in an attribute selector, to find the linked textarea.
$('button').click(function(){
alert( $('textarea[for="' + $(this).attr('value') + '"]').val() );
});
It would be better to use data-*
attributes for your custom data.
Upvotes: 2