C
C
corona-net2020-04-28 14:32:27
JavaScript
corona-net, 2020-04-28 14:32:27

How to correctly enter a variable into a script?

Good day.
There is a variable, let's say xyz = "www.site.com/wp-content/themes/Stars/";
How to enter it before src in this line? So that she adds the address I need?
$("#img").attr("src",response);

Here is a sample code to do this:

$(document).ready(function(){

        $("#but_upload").click(function(){

            var fd = new FormData();
            var files = $('#file')[0].files[0];
            fd.append('file',files);

            $.ajax({
                url: '/wp-content/themes/Stars/upload.php',
                type: 'post',
                data: fd,
                contentType: false,
                processData: false,
                success: function(response){
                    if(response != 0){
                        $("#img").attr("src",response);
                        $(".preview img").show(); // Display image element
                    }else{
                        alert('file not uploaded');
                    }
                },
            });
        });
    });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Sokolov, 2020-04-28
@corona-net

So?
var url = " https://www.site.com/wp-content/themes/Stars/ ";
$("#img").attr("src", url + "image1.png");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question