简体   繁体   中英

How to use EJS inside HTML attributes?

I have the following two cases where I want to use an EJS tag inside an HTML attribute.

CASE 1:

<input
   style="background-image: url(<%= img.URL %>)"
/>

CASE 2:

<input
    onchange="handleCheckboxChange(<%= img.URL %>)"
/>

I am unable to figure out why is it not working. The img object is passed correctly while rendering the template.

Case 2 translates to the following:

<input
    onchange="handleCheckboxChange(https://example.org)"
/>

Which is a syntax error

You need to wrap them inside quotes:

<input
    onchange="handleCheckboxChange('<%= img.URL %>')"
/>

I found a solution for the same, the ejs tags need to be wrapped inside quotes .

SOLUTION FOR CASE 1

<input
   style="background-image: url('<%= img.URL %>')"
/>

SOLUTION FOR CASE 2

<input
    onchange="handleCheckboxChange('<%= img.URL %>')"
/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM