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

  • Home
  • SEARCH
  • 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 9007915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:49:01+00:00 2026-06-16T01:49:01+00:00

I created a function and then called this function when the page loads, also

  • 0

I created a function and then called this function when the page loads, also I need call same the function, but without it existing twice. I want it to just stop from $(function(){}) and call again when an element is clicked on.

function myfunction(){
    console.log('message');
}

$(function(){
    myFunction();

    $('#id').click(function(){

     ...some code here ...
     myFunction();

});
})

When page is loaded the console gives me: “message” – it’s ok, but when click on #id then I get this message twice, if again then 3 times;


Here my code

function select_cta(){
    $('.cta-id').click(function(){
        console.log('-');
        $('.cta-id').removeClass('active');
        $(this).addClass('active');

        var cta_arr = parseInt($(this).attr('id').replace('cta-button-', ''))-1;

        $('.cta-image').fadeOut(300).removeClass('active');
        $('#'+$(this).attr('id').replace('cta-button', 'cta-image')).fadeIn(300).addClass('active');
        if(cta_data[cta_arr]){
            $('.cta-actions #cta_id').val(cta_data[cta_arr].id);    
        }
        else{
            $('.cta-actions #cta_id').val('');
        };

        if(cta_data[cta_arr]){          
            $('.cta-actions #cta_link').val(cta_data[cta_arr].link);
        }
        else{
            $('.cta-actions #cta_link').val('');
        };

    });

}

$(function(){

    select_cta();

    $('.add-new-cta').click(function(){
        var new_tab = parseInt($(this).prev().attr('id').replace('cta-button-',''))+1;
        $(this).before('<div id="cta-button-'+new_tab+'" class="cta-btn cta-id empty" style="display:none;"><span>'+new_tab+'</span><span class="onair">ON</span></div>');
        $('#cta-button-'+new_tab).fadeIn(300);
        $('.cta-images').append('<div id="cta-image-'+new_tab+'" class="cta-image " style="display:none"><img src="/assets/images/page/placeholder_cta.gif"></div>');

        select_cta();   

    })
});
  • 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-16T01:49:03+00:00Added an answer on June 16, 2026 at 1:49 am

    Your problem that every call to select_cta adds another handler to each of the elements. They all would be executed when the click event fires. Two solutions:

    • Unbind the event handlers from all elements before you re-add them. To do so, begin the function select_cta with $('.cta-id').off("click").on("click", function(){…
    • Better: use event delegation:
    jQuery(function($){
        function getIdNumber($el) {
            return parseInt($el.prop('id').replace(/\D/g, ''), 10);
        }
        var $active = $('.cta-id.active'),
            $activeImg = $('.cta-image.active')
    
        $(document).on("click", '.cta-id', function(e) {
            $active.removeClass('active');
            $active = $(this).addClass('active');
    
            var num = getIdNumber($active);
    
            $activeImg.fadeOut(300).removeClass('active');
            $activeImg = $('#cta-image'+num).fadeIn(300).addClass('active');
    
            var cta_arr = num - 1;
            if(cta_arr in cta_data) {
                $('#cta_id').val(cta_data[cta_arr].id);    
                $('#cta_link').val(cta_data[cta_arr].link);
            } else {
                $('#cta_id').val('');
                $('#cta_link').val('');
            }
        });
    
        $('.add-new-cta').click(function(e) {
            var $this = $(this),
                new_tab = getIdNumber($this.prev())+1,
                new_button = $('<div id="cta-button-'+new_tab+'" class="cta-btn cta-id empty" style="display:none;"><span>'+new_tab+'</span><span class="onair">ON</span></div>');
            $this.before(new_button);
            new_button.fadeIn(300);
            $('.cta-images').append('<div id="cta-image-'+new_tab+'" class="cta-image " style="display:none"><img src="/assets/images/page/placeholder_cta.gif"></div>');
        })
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a header file called ofdm.h which includes all the function prototypes. Then
I declared a delegate: public delegate void Del(string message); Then i created a function
Ive created a function which returns all the obserables items in my html page.
I created a function which loads a very basic map file of the form:
I created a function for splitting an image into multiple images, but when I
I just got a fix on another post I just made but this created
right now when it first loads the html page, my checkbox was created in
I've created a page that lists information in a table, this information comes from
I've created this test page to bring out the issue I'm encountering. The first
I am trying to create a function which will take arguments arg1, arg2... then

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.