1

Hello i am having difficulty achieving this task, i know how to pass a normal variable but for some reason i can't find a way to make this work.I am having this jquery code

for(let i=0;i<data.length;i++) {
  const newRow = `<tr>
      <td id="commentid">${data[i].id}</td>
      <td id="username">${data[i].userName}</td>
      <td id="usermail">${data[i].email}</td>
      <td id="createdat">${data[i].createdAt}</td>
      <td id="commentmessage"><button type="button" class="btn btn-info btn-fw"
                                                        onclick="alert(${data[i].message})">Show
                                                </button></td>
      <td id="deletecomment">
<a href="/admin/dashboard/showposts/deletecomment/${postid}/${data[i].id}" style="text decoration:none;"class="btn btn-danger btn-fw mx-auto" onclick="return confirm('Are you sure you want to delete this comment?');">Delete Comment</a></td>
      </tr>`

  $("#commentstable").append(newRow)
    }

I am not sure if i pass the

${data[i].message}

correctly to the alert function. Also as you can see the whole block of code is a string assigned to the newRow variable. Now each time i am running the application and click the button i am geting this error

Uncaught SyntaxError: missing ) after argument list

Where is this error comming from?Where do i miss )? it seems that i am having all the ), how can i make this work? Any help would be greatly appreciated !

L.E I am adding the whole function that contains this loop

function openModal2(data,postid){
$("#mysimpleModal2").css("display","block")

//This removes the previous data that is added by the loop.html() works with jquery.
$("#commentstable").html('');


 for(let i=0;i<data.length;i++) {
  const newRow = `<tr>
      <td id="commentid">${data[i].id}</td>
      <td id="username">${data[i].userName}</td>
      <td id="usermail">${data[i].email}</td>
      <td id="createdat">${data[i].createdAt}</td>
      <td id="commentmessage"><button type="button" class="btn btn-info btn-fw"
                                                        onclick="alert('${data[i].message}')">Show
                                                </button></td>
      <td id="deletecomment"><a href="/admin/dashboard/showposts/deletecomment/${postid}/${data[i].id}"
                                                   style="text-decoration:none;"
                                                   class="btn btn-danger btn-fw mx-auto"
                                                   onclick="return confirm('Are you sure you want to delete this comment?');">Delete
                                                   Comment</a></td>
    </tr>`

  $("#commentstable").append(newRow)
    }
}

1 Answer 1

3

You need add single quote ' to wrap the parameter

onclick="alert('${data[i].message}')"
Sign up to request clarification or add additional context in comments.

6 Comments

thanks, the thing is now i am geting this error Uncaught SyntaxError: Invalid or unexpected token
@helloApp This error has something to do with your server code or other code, you need to check it on the related code,you need to add the code of alert() function
server code is on java and is working ok, what does this error actually mean?where should i check for it?
@helloApp You need add more details of your code, otherwise no one can figure out the reason
i have added the whole function that works with that loop, please let me know if you want me to add some more code
|

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.