0

I have following javascript code:

 var table = "<tr>" +
    "<td style='width:48%' class='ads-details-td'><div>" +
    "<p><strong> <a href='#' onclick='showServiceInfo(" + data.services[i].service_name + ")'"+ data.services[i].service_name + "</a> </strong></p>" +
    "</div></td>" +
    "<td style='width:16%' class='price-td'><div><strong> <input type='number' min='1' value='1' id='serviceQuantity"+data.services[i].service_identification+"' onchange='computePrice("+ data.services[i].service_identification+")' ></strong></div></td>" +
     "<td style='width:16%' class='price-td'><div><strong><p id='servicePrice"+data.services[i].service_identification+"'>"+data.services[i].service_price+"</p> </div></td>" +
      "<td style='width:10%' class='action-td'><div>" + 
        "<p> <a class='btn btn-primary'> <i class='fa fa-shopping-cart'></i> Add </a></p>" +
        "</div></td>"

and my javascript function is

function showServiceInfo(x)
{
    alert(x)
    console.log(x)
}

Here

data.services[i].service_name

is a variable I am fetching from ajax request. I am unable to understand what i missed or added extra )

2
  • add up a jsfiddle of your question for better understanding. Commented Jan 29, 2016 at 21:11
  • You're also missing the closing </tr> tag Commented Jan 29, 2016 at 23:45

1 Answer 1

2

You are not missing a ), however, you are missing the > for the link. You are also missing quotes around the variable being put into the function, and if the value data.services[i].service_name is a string that is not the name of an variable, this will also cause an error because that variable is undefined.

"<a href='#' onclick='showServiceInfo(" + data.services[i].service_name + ")'"+ data.services[i].service_name + "</a>"

should be

"<a href='#' onclick='showServiceInfo(\"" + data.services[i].service_name + "\")'>"+ data.services[i].service_name + "</a>"
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply. I add your code but still getting same error.
Yea, I thought it might be something else. That's why I hesitated to close-vote for typographical error and actually test what I wrote. All I got was that without the closing bracket, the table is malformed and the link isn't even visible, but no console errors. With the bracket, I get an error about the variable not being defined. Will be editing with a fix for that. But I don't get the same error as you.
I changed the value of data.services[i].service_name. I create a variable and assign value to it and pass it to the function. It's working perfect but whenever I tried to pass value from JSON response i am getting same error.
All I can suggest at the moment is to create a jsfiddle as suggested earlier

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.