• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

Sending variables in Javascript

inside a function, I have code that generates html that includes a javascript function that sends a variable to another function.

It looks like this:

searchVidResult += '<div class="triovid lastvid"><a href="#" onclick="swapvids('+ finalID +');">....

Open in new window

The issue is, the finalID variable (a string) isn't being quoted correctly, so i get an syntax error or an undefined object error.
how do I get finalID sent to sawpvids?

Thanks for your help!
1 Solution
Where in your code does it say:
var finalID

Open in new window

Remove "var " and then add this to the very top above all other JavaScript code:
var finalID;

Open in new window

Dave BaldwinFixer of ProblemsCommented:
Your example is missing a final single quote.  In addition, you may need quotes around '+ finalID +' but you have already used both single and double quotes.  I'm not sure how to resolve that.
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
besides missing the final quote as others pointed out, you need to escape the single quote inside your swapvids() function. to do that, you would use

\' - ex: alert(top O\' the mornin\' to ya!');

so in your code, it should be:

searchVidResult += '<div class="triovid lastvid"><a href="#" onclick="swapvids( \' '+ finalID +' \');">....

Open in new window

i put spaces in between the quotes for readability.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now