Search code examples
javascriptjqueryhtmlthymeleaf

Passing multiple parameters to a javascript function from thymeleaf tag


I'm very new to thymeleaf. Here i have stuck in passing parameter. Here is my html page.

<tr th:each="result : ${searchResult}">
<td>
    <a href="#" th:text="${result.getString('type')} +'|'+  ${result.getString('name')} +'|'+  ${result.getString('revision')}"></a>
</td>
<td>
    <a href="#" role="button" class="green" data-toggle="" onclick="dataSearchAjax1('Source','sourceResultDiv')">view</a>
</td>
</tr>

This is my javascript function

function dataSearchAjax1(searchType, resultDiv) {
        var typeVar=searchType;
        $.ajax({
            url : 'dataSearchAjax1',
            data: {type:typeVar},
            success : function(data) {
                $('#'+resultDiv).html(data);
            }
        });
    }

Here i Have to pass result.getString('type') and result.getString('name') instead of 'source' and 'sourceResultdiv'.

I have tried

 th:onclick="'javascript:dataSearchAjax1(\'' + ${result.getString('type')},${result.getString('name')} + '\');'"

Also i tried with th:attr="online...tag..Both are not working. Can somebody please help me?


Solution

  • You need to escape the , separator also, so the code to perform the function call would be:

    th:onclick="'javascript:dataSearchAjax1(\'' + ${result.getString('type')} +'\',\''+ ${result.getString('name')} + '\');'"