Background: I have 10 html rows I am creating via a php loop each of the rows will have a "more info" link. I am parsing JSON data into the html as well. My question is for the more info links they need to trigger a javascript function and pass variables that are associated with each specific row.
Question: Does it make more sense to add the onClick for each more info link like so (EXAMPLE 1):
<a href="#" onclick="return myFunction('123', 'food');">More Info</a>
or can I write on javascript eventlistener that will fire for each more info link and some how I would pass my variables? (EXAMPLE 2)
<a href="#" id="link_1">Click me</a>
$("#link_1").on('click', function() {
});
Below is my PHP that creates the rows and the links.
$json = json_decode($data, true);
$cleanHTML = "";
foreach($json['restaurants'] as $val){
$id = $val['restaurant']['id'];
$sectionType = "food";
$cleanHTML .= "<div data-content>
<article>
<div class='grid-container'>
<div class='MainTitle'>". $val['restaurant']['name'] ."</div>";
//THIS IS WHERE I NEED TO CREATE THE MORE INFO LINK
$cleanHTML .= "<div class='VenueDescription'>";
if(SwpmMemberUtils::is_member_logged_in()) {
//THIS IS EXAMPLE ONE ABOVE:
$cleanHTML .= "<a href='#' onclick="return myFunction('". $id ."', '". $sectionType ."');">More Info</a>";
//OR THIS IS A TRY AT EXAMPLE TWO ABOVE:
$cleanHTML .= "<a href='#' id='". $id ."'>More Info</a>";
} else {
$cleanHTML .= "Click Here to Become a Member!";
}
$cleanHTML .=" </div>
</div>
</article>
</div>";
<a data-123="123" data-food="qweqwe" />and get them in the javascript using$(this).data('123')?