• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Call Ajax everytime the button is clicked

The Raven

Coder
My code:

JavaScript:
$(document).ready(function() {

    // Random images generated thanks to free API: https://github.com/jigsawpieces/dog-api-images


    $.ajax({
        url: 'https://dog.ceo/api/breeds/image/random', // Replace with your JSON API URL
        type: 'GET',
        dataType: 'json',
        success: function(data) {
            $('<img src="https://i.ibb.co/Wv5Jjb8/3316536-animal-cachorro-dog-dogs-icon-1.png" class = "doggo" />').insertAfter('.quick-reply-textarea-wrap .sceditor-group').click(function() {
                $('#text_editor_textarea').sceditor("instance").insertText('\n\n[img]' + data.message + '[/img]');
            })
        },
        error: function(error) {
            console.error('Error fetching JSON:', error);
        }
    });

});

I tried to put complete ajax in the function and separate it from this:
JavaScript:
            $('<img src="https://i.ibb.co/Wv5Jjb8/3316536-animal-cachorro-dog-dogs-icon-1.png" class = "doggo" />').insertAfter('.quick-reply-textarea-wrap .sceditor-group').click(function() {

                $('#text_editor_textarea').sceditor("instance").insertText('\n\n[img]' + data.message + '[/img]');

            })

However, the current code I have, runes only once this request. And I can press as much as I want, I always get the same image. This API generates random dog images. But my code generates only once. How to call it everytime button is clicked?
 
Solution found. Had to add another function around it like this:

JavaScript:
function callImage() {
 
  // Random images generated thanks to free API: https://github.com/jigsawpieces/dog-api-images

    $.ajax({
        url: 'https://dog.ceo/api/breeds/image/random',
        type: 'GET',
        dataType: 'json',
        success: function(data) {

            $('#text_editor_textarea').sceditor("instance").insertText('\n[img]' + data.message + '[/img]');

        },
        error: function(error) {
            console.error('Error fetching JSON:', error);
        }
    });

}

$(document).ready(function() {

    $(function() {
        $('<img src="https://i.ibb.co/Wv5Jjb8/3316536-animal-cachorro-dog-dogs-icon-1.png" />').insertAfter('.sceditor-group:last').on("click", function() {
            callImage();
        });

    });

});
 
Top Bottom