1

I'm trying to develop a web application with Thymeleaf and I've created an html page that uses an external JavaScript file to change an image URL.But the standard syntax URL:

document.getElementById("im1").src="images/img1.jpg" ;

does not work.Everything else in the JavaScript code works fine.What kind of URL syntax should I use?Thanks in advance.

0

1 Answer 1

8

Try something like :

<script th:inline="javascript">
    /*<![CDATA[*/
        var context = [[@{/}]];
    /*]]>*/

    document.getElementById("im1").src=context + 'images/img1.jpg' ;

</script>

Maybe your code didn't reach the image in the context. Debug or use console to see the URL generated from: context + 'images/img1.jpg'. Then try to acess it via your browser. Normaly it should display the image.

Sign up to request clarification or add additional context in comments.

2 Comments

Why the brackets around the thymeleaf expression?? @Jadenko88
Because this is required syntax. See documentation for more info.

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.