0

How to assign a variable value in javascript and use it in html?

My javascript code:

function validateUrl() {
    var displayButton = "Yes";
}

My html code:

<c:if test="${displayButton == 'Yes' "
</c:if>

How can i use a javsacript variable in my html?

3
  • you could not do like this. Which technical language you are using like .net, php or JAVA ? Commented Apr 6, 2016 at 11:34
  • if you want to use dynamic value in html, you need to use either any scripting language like php,java,C#, etc... or you need to use the front end jsframework like Angular.js, knockout.js etc.. Commented Apr 6, 2016 at 11:40
  • Have you invented that <c:if> tag yourself, or is there some templating system which actually handles that? If so the problem simply becomes when Javascript executes and when the template is parsed. Usually Javascript executes after templates are parsed, soooo... no. Commented Apr 6, 2016 at 11:57

2 Answers 2

1

This is not HTML, it is a JSTL-Tag.

The code <c:if will be evaluated by the server (glassfish/tomcat) while producing the HTML which will be provided to the browser.

The JavaScript code runs in the browser. There is no (simple) way to use JavaScript values to manipulate HTML-rendering.

HTML itself isn't able to work with JavaScript-variables, so the only way is, to set a variable in JavaScript and use the value with JavaScript.

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

Comments

0

You can set data using jquery and $("#display-button").data("key", "Yes") you can read the data out using $("#dispaly-button").data("key") -> returns "Yes"

2 Comments

how to use in if condition ?
this way only works if you use JavaScript to manipulate your HTML the mentioned method can be used only on the client site to store data in HTML..

Your Answer

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