Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9165587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:56:33+00:00 2026-06-17T14:56:33+00:00

I am using a masonry type code to display Divs on my page, and

  • 0

I am using a masonry type code to display Divs on my page, and using an API to get the data so everything is loaded via html+= into the DOM.

e.g. Each div is loaded like this

html += '<li><div class="classname>content';
html += '<div id="like'+image.article_id+'">';
html += '<a href="#" class="like" id="'+image.article_id+'">';
html += '<div class="bLike" title="Like this article"></div></a></div>';
html += '<div id="unlike'+image.article_id+'" style="display:none;">';
html += '<a href="#" class="unlike" id="'+image.article_id+'">';
html += '<div class="bUnlike" title="Unlike this article"></div></a></div>';
html += '</div></li>';

Now, I am using a like button within the results shown which uses ajax to update my database to allow the user to see what they have liked. Simple.

So, to get the ajax code into play I have to load it into the DOM which I do as below, I load this after the divs example shown above:

html += '<script type="text/javascript">';
     html += '$(function()';
     html += '{';
     html += '$(".like").click(function(){';
     html += 'var element = $(this);';
     html += 'var I = element.attr("id");';
     html += 'var info = \'wish_id=\' + I;';
     html += '$(\'#like\'+I).hide();';
     html += '$(\'#unlike\'+I).show();';
     html += '$.ajax({';
     html += 'type: "POST",';
     html += 'url: "/pages/includes/ajax/like.php",';
     html += 'data: info,';
     html += 'success: function(){';
     html += '}';
     html += '});';
     html += 'return false;';
     html += '});';
     html += '});';
 html += '</script>';
 html += '<script type="text/javascript" >';
     html += '$(function()';
     html += '{';
     html += '$(".unlike").click(function(){';
     html += 'var element = $(this);';
     html += 'var I = element.attr("id");';
     html += 'var info = \'wish_id=\' + I;';
     html += '$(\'#unlike\'+I).hide();';
     html += '$(\'#like\'+I).show();';
     html += '$.ajax({';
     html += 'type: "POST",';
     html += 'url: "/pages/includes/ajax/unlike.php",';
     html += 'data: info,';
     html += 'success: function(){';
     html += '}';
     html += '});';
     html += 'return false;';
     html += '});';
     html += '});';
 html += '</script>';

Now, when a user clicks like, or unlike, it fires twice, therefore hitting the DB twice and basically doubling the whole workload which is never good.

So, I’m thinking that maybe the script shown above has been loaded into the DOM twice. So I go to firefox firebug and look at the code loaded into the DOM. All the divs are there that I loaded using the same html+= but not the script above. I know its there as it works prefectly.

So why can I not see it, and why is it firing twice?

Is there a better way to do this?

EDIT:

This is the API which loads the page:

function loadData() {
  isLoading = true;
  $('#loaderCircle').show();

  $.ajax({
    url: apiURL, // fetches from MySQL
    dataType: 'json', // data type
    data: {page: page}, // page number so each request brings in next records
    success: onLoadData // loads the data which is looped and html+= as at top of post
  });
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:56:34+00:00Added an answer on June 17, 2026 at 2:56 pm

    So, what you should do is simply fire JavaScript instead of adding <script> tag and remove $(function(){...}) wrapping, because code inside will be executed only on ready event which fires only once, when DOM is ready.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using sortable to sort different width x height divs and masonry to clean
I'm using masonry for layout. I have set up a filter for the divs
Using System.Diagnostics.EventLog .NET type one can programmatically create logs into the Event Viewer application.
I've managed to get multiple filters working using the jQuery Masonry plugin, the only
I'm using jquery isotope to get the 'masonry' effect but having issues. Firstly, the
I am using infinite scroll in conjunction with Jquery Masonry. Everything is working great,
I'm using masonry (jQuery plugin) to create fluid awesome columns . Everything worked great
I am using the Masonry. I have the fallowing code after masonry loads the
I'm using Masonry and Embedly plugin to display embedded content, I've put together an
I'm using the Masonry plugin to float elements on a page, and it uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.