Why JavaScript method not get called?

I have this is html:

<script>
function decode(str) {
    return decodeURI(str);
}
</script>

<span>decode('Alma%20k%C3%B6rte%20di%C3%B3')</span>

Why it is not get called?


Solution 1:

HTML is a markup language and does not directly execute programmatic code as part of document content.

You can execute JS code in a <script> tag and use DOM methods to manipulate the document

<script>
function decode(str) {
    return decodeURI(str);
}
</script>

<span></span>

<script>
// locate the <span> element and update its `textContent` property
document.querySelector("span").textContent =
  decode('Alma%20k%C3%B6rte%20di%C3%B3')
</script>

Solution 2:

HTML does not support direct execution of JS on the markup, So in case you want to render data on request, you will have to pass the function bound to a event listener like ex : (onclick)

Your code should be like as follows

<!DOCTYPE html>
<html>
<body>

<button onclick="decode()">decode data</button>

<span id="foo"></span>

<script>
function decode(str) {
    document.getElementById("foo").innerHTML = decodeURI(str);
}
</script>

</body>
</html>

You can wrap to any event listeners mentioned in the MDN docs