get the value of the class jquery -


i beginner in jquery, problem is

<?php for($i=0;$i<3;$i++) { ?> <form> <input type="text" class="s" value="v<?php echo $i; ?>" /> <input type="button" value="ok" onclick="test();" /> </form> <?php } ?>   <script language="javascript"> function test(){ var a= $(".s").val(); alert(a); } </script> 

i want value of text click button, if click first or second or last button show me 'v0' all. want have 'v0' if click first button, 'v1' second , 'v3' last.

i think u understand want explain because again beginner in english

since using jquery bind event using it. can add css class button , bind event using it.

html

<form> <input type="text" class="s" value="v<?php echo $i; ?>" /> <input class="btn" type="button" value="ok" /> </form> 

script

$(function() {     //bind event using btn class added button     $('.btn').click(function(){         //use .prev() find prev input         var = $(this).prev(".s").val();         alert(a);     }); }); 

refrences

  1. document ready handler

    specify function execute when dom loaded.

  2. class selector (“.class”)

selects elements given class.

  1. .click()

bind event handler "click" javascript event, or trigger event on element.

  1. .prev()

    get preceding sibling of each element in set of matched elements, optionally filtered selector.


Comments

Popular posts from this blog

symfony - TEST environment only: The database schema is not in sync with the current mapping file -

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -