0

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>";
4
  • 1
    for you ex2: You can use data properties. <a data-123="123" data-food="qweqwe" /> and get them in the javascript using $(this).data('123') ? Commented Oct 16, 2019 at 2:56
  • Thanks @RooshanAkthar that was the piece I was missing. Does either example have a downside or a reason not to use it in one of the above ways? Commented Oct 16, 2019 at 4:05
  • First example too much repetition. Imagine a situation you want to add one more argument you might have to change in few places. 2nd one has the entire freedom on adding any events in the future without changing the html. Commented Oct 16, 2019 at 4:10
  • @RooshanAkthar if you want to post that as an answer that is the info I was looking for. You make a good point. Example two was the way I wanted to to get it working. Example 1 seemed like it was taking the long way. Thanks! Commented Oct 16, 2019 at 4:12

1 Answer 1

1

Example two is the way to go as example one lacks future-proofing to an extent. Imagine a situation where you have to add more arguments or changes which needs to be addressed in several places.

For the arguments on example two you can use data attributes. <a data-123="123" data-food="qweqwe" /> and access them using appropriate data methods. For example in jQuery $(this).data('food')

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

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.