Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 968
Arun Palanisamy Avatar asked Jan 29 '15 06:01

Arun Palanisamy


1 Answers

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')} + '\');'"
like image 161
Leandro Carracedo Avatar answered Oct 20 '22 00:10

Leandro Carracedo