0

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.

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

try this

$(document).ready(function(){
    $(".test button").click(function(){
        alert($(this).parent().find('textarea').val());
    });
});

See Fiddle

Comments

0

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>

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.