Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Syntax Error Onclick

Status
Not open for further replies.

TimBiesiek

Programmer
Nov 3, 2004
151
AU
Hi all,

I am a bit of a newbie to Javascript, only starting to use it now with AJAX.

I have an ASP page that lists products, and next to each product is a button. I want this button to call a Javascript function that updates a div area that shows an included ASP page (The shopping cart). The code I have is:

Code:
response.Write("<td><button type='button' onclick='RefreshCart('Add', "[red] & RsProducts("ProductID") & [/red]", 1)'>Add to Cart</button></td>")

The part in red just shows an ASP part of the page that returns the Product ID.

When the page loads it shows an error notification at the bottom of the IE page, stating:
Code:
Line: 120
Char: 1
Error: Syntax Error
Code: 0

If I take out the onclick event bit, the page loads fine, but it obviously doesn't do what I want when the button is clicked.

Can anyone help me here? I've tried all sorts...

Thanks!
 
You are having quoting issues:

Code:
onclick=[red]'[/red]RefreshCart([red]'[/red]Add[red]'[/red], " & RsProducts("ProductID") & ", 1)[red]'[/red]

You open single quotes to contain the javascript that should be run inside the onclick event, but then you proceed to open single quotes again inside it. That doesn't work.

So for the browser, your onclick event is just:
Code:
onclick=[red]'[/red]RefreshCart([red]'[/red]

Which is of course wrong.

So either escape the single quotes, or change them to double quotes and it should work.
Code:
onclick=[red]'[/red]RefreshCart([blue]"[/blue]Add[blue]"[/blue], " & RsProducts("ProductID") & ", 1)[red]'[/red]


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Ahhh, true... I thought it would probably be something simple that I was overlooking... Only problem with changing to double quotes is that it is in a response.write, so I may take it out of the .write to get around this...

Will try it and let you know how it goes! Thanks!
 
You can try to escape the quotes instead with a forward slash.
Code:
\' Add \'

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top