Search code examples
javascriptsyntax-errorhrefidentifier

Unexpected identifier in a href


I want to call an AJAX function in a onclick method of a <a href.. > but I can't.
The problematic code is the following:

<div class="container" id="<portlet:namespace/>tabla">          
    <table class="noticias-table">
        <thead>
            <tr class="thead-agregador">
                <th scope="col">Imagen</th>
                <th scope="col">Fecha</th>
                <th scope="col">Categoría</th>
                <th scope="col">Titular</th>
                <th scope="col">Estado</th>
                <th scope="col">Opciones</th>
            </tr>
        </thead>
        <tbody>
            <c:forEach var="contenido" items="${Contenidos}">
                <fmt:formatDate pattern="dd/MMM/yyyy" value="${contenido.fecha}"
                    var="formatFecha" />
                <tr>
                    <td class="td-contenido"><img src="${contenido.imagen}"
                        class="td-contenido-img" /></td>
                    <td class="td-contenido">${formatFecha}</td>
                    <td class="td-contenido">${contenido.categoria}</td>
                    <td class="td-contenido"><a id="tituloContenido" href="#"
                        onclick="openNews(${contenido.articlePrimKey}); return false">${contenido.titulo}</a>
                    </td>
                    <td class="td-contenido">${contenido.status}</td>
                    <td class="td-contenido">Opciones rellenar</td>
                </tr>
            </c:forEach>
        </tbody>
    </table>
    <a id="Ultimas" href="#" onclick="sortByLast(${Contenidos}); return false">Ultimas</a>
</div>

The variable "Contenidos" that I want to pass to the controller y a List<PersonalBean>, using the developer tools of the browser I can see that the list is properly formatted in the <a> call, but when I click on it, the Uncaught error appears and the script methods don't start the execution

If I remove the method onclick, the error disappears, what is wrong in that sentence?


Solution

  • I am guessing that is a templating syntax and you are replacing it with a string so you need quotes

    sortByLast('${Contenidos}');