0

I need to call a javascript function inn thymeleaf and I am getting an error as function is not defined. Here's my code

<div class="input-field col s6 m6">
        <input id="isbn" name="isbn" type="text" class="validate" />
        <label for = "isbn">Enter ISBN Code</label>
</div>
    <div class="input-field col s6 m6">
        <button id="submitCode" class="btn waves-effect waves-light col m4" th:onclick="'javascript:myFunction();'" value="data">ISBN Data</button>
    </div>

Javascript Code

function myFunction()
{
    var isbn = document.getElementById('isbn').value;
    alert(isbn);
    var xmlhttp = new XMLHttpRequest();
    var url = "https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn;
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200)
        {
            var x = JSON.parse(xmlhttp.responseText);
            callback(x);
        }
    };
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}
function callback(x)
{
    //do things with your data here
    alert(JSON.stringify(x));
    console.log(x);
}
chan
  • 274
  • 1
  • 5
  • 24

1 Answers1

1

May be you should try th:onclick="'myFunction();'" You can refer this link Javascript function call with Thymeleaf

Sachin Kumar
  • 808
  • 3
  • 11
  • 29