passing javascript variable to button

hi, i currently have the top code snippet and it works fine, however i would like it to call a function with the given value instead of opening it as a URL, i attempted to do this with the second code snippet - however i cannot get it to work...

all i want to do is pass the value of service["links"][0]["href"] to the function previewFunction() in that string...

it must be a super simple answer... but something i am just missing all morning... any advice much appreciated...
Thanks!

     linkString += '<li><a href="'+service["links"][0]["href"]+'">'+linkStringArray["type"]+'</a></li>'; //shows url successfully

Open in new window

                             
      

  linkString += '<li><button onclick="previewFunction("'+ service["links"][0]["href"] +'")">g</button></li>';

Open in new window

LVL 1
weaverkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
How does it not work?

One thing is you have double quotes in your string twice i.e.
linkString += '<li><button onclick="previewFunction("'+ service["links"][0]["href"] +'")">g</button></li>';

Open in new window

Which will render as
<li><button onclick="previewFunction("somevalue")">g</button></li>

Open in new window

Which is going to fail page validation

Try this
linkString += '<li><button onclick="previewFunction(\''+ service["links"][0]["href"] +'\')">g</button></li>';

Open in new window

Which should render as
<li><button onclick="previewFunction('somevalue')">g</button></li>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
weaverkAuthor Commented:
perfection, i knew it was something simple like that... but i just could not see it...
many thanks for this quick response!
0
Julian HansenCommented:
You are welcome - thanks for the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.