• Resolved downfast

    (@downfast)


    Hi Darren
    I need to be able to load some js code to work inside the results:
    I currently do this in the template, yet this will repeat the code for each result, is there a better way you know of?

    <?php global $post; ?>
    <li>
    
       <h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    	<br>
       <?php the_field("country"); ?><br>
    	<?php the_field("year"); ?>
    	<br>
       <?php the_excerpt(); ?>
    	<button id="wiki" data-subject="<?php the_title(); ?>">Wikipedia</button>
    		<script>
    			$("#wiki").on('click', function(e) {
    			  var q = $(this).data('subject');
    			  $.getJSON("https://it.wikipedia.org/w/api.php?callback=?", {
    			    srsearch: q,
    			    action: "query",
    			    list: "search",
    			    format: "json"
    			  }, function(data) {
    			    $("#results").empty();
    			    $("#results").append("<p>Results for <b>" + q + "</b></p>");
    			    $.each(data.query.search, function(i, item) {
    			      $("#results").append("<div><a href='https://it.wikipedia.org/wiki/" + encodeURIComponent(item.title) + "'>" + item.title + "</a><br>" + item.snippet + "<br><br></div>");
    			    });
    			  });
    			});
    		</script>
    	<output id="results"></output>
    </li>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter downfast

    (@downfast)

    Resolved by using the callback functions provided

    $(function() {
      $.fn.almComplete = function(alm){
    $("#wiki").on('click', function(e) {
    			  var q = $(this).data('subject');
    			  $.getJSON("https://it.wikipedia.org/w/api.php?callback=?", {
    			    srsearch: q,
    			    action: "query",
    			    list: "search",
    			    format: "json"
    			  }, function(data) {
    			    $("#results").empty();
    			    $("#results").append("<p>Results for <b>" + q + "</b></p>");
    			    $.each(data.query.search, function(i, item) {
    			      $("#results").append("<div><a href='https://it.wikipedia.org/wiki/" + encodeURIComponent(item.title) + "'>" + item.title + "</a><br>" + item.snippet + "<br><br></div>");
    			    });
    			  });
    			});
      };
    })(jQuery);
    • This reply was modified 8 years, 4 months ago by downfast.
    Plugin Author Darren Cooney

    (@dcooney)

    Nice work!
    Was there another support issue you were waiting on a response?

    With the new forums I can’t sort by response date so i’m losing which tickets are still active.

    Thread Starter downfast

    (@downfast)

    hey no that’s fine man, thanks a lot

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Load jquery code to work after results’ is closed to new replies.