0

I'm trying to create an html tag using the following Jquery

$("<option />",{ 'data-src':"{{ asset(my-javascript-variable) }}", id:'my_id').appendTo($('#image'));

This is an option tag which I am appending to a select. This is occurring on the success of an ajax call within a for loop.

How can I construct a URL using the asset() method with a javascript variable? Is this even possible?

1 Answer 1

1

You can not do that, since PHP will be executed and interpreted first and then the browser will interpret javascript.

But you can try this:

var myJavascriptVariable = 'blank.jpg';
$("<option />",{ "data-src": `{{ asset('img/') }}${myJavascriptVariable}`, "id": "my_id"}).appendTo($('#image'));

Or

var baseUrl = "{{ asset('img/') }}";
var myJavascriptVariable = 'blank.jpg';
$("<option />",{ "data-src": `${ baseUrl + myJavascriptVariable}`, "id": "my_id"}).appendTo($('#image'));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.