Weverton Souza Weverton Souza - 1 year ago 47
jQuery Question

Know which line was clicked within the php loop

I have a while loop in php that returns me some data from my mysql database, this loop creates several ul dynamic read that return me the lines surveyed in the bank, each row returns 1 button and a imput type hidden that contains a link its value, I would like to know the link for each button clicked

for example the list returns me 10 lines

each line will have a hidden imput with the value of a link

I wonder what is the link home value so I click the button btlink



<? Php while ($ videolist = $ resultvideos-> fetch_array ()) {?>



<Li class = "listdivvideos">

<Button class = "btlink" id = "btlink"> </ button>

</ Li>


<Input type = "hidden" class = "linkvd" value = "<? Php echo $ videolist [ 'link']?>"> </ Input>

</ Li>

</ Ul>

</ Li>
</ Ul>

<? Php}?>

I tried using this code but it just returns me the first link

$('.listdivvideos').on('click', () => {

var linkdovideo = $('.linkvd').attr('value');



Answer Source

First of all you don't need input elements for this, you could do something like this:

<?php while ($ videolist = $ resultvideos-> fetch_array ()) { ?>
    <button class="btlink" data-value="<?php echo $ videolist['link'] ?>">Click me</button>
<?php } ?>

And then in your jQuery code:

$(".btlink").click(function() {