7

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?

2 Answers 2

11

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')} + '\');'"
Sign up to request clarification or add additional context in comments.

Comments

1

This works for me, easy and clear to use [[ ]]

Pass multiple parameters:

<button type="button" id="editUserButton" class="btn btn-primary" th:onclick="editUser([[${user.getId}]],[[${user.getLastName}]])">Edit</button>


th:onclick="dataSearchAjax1([[${result.getString('type')}]] ,[[ ${result.getString('name')}]])"

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.