Skip to main content
Inspiring
July 19, 2006
Answered

please help with some basic javascript syntax

Can anyone please help me with a "remove from favourites" facility i'm working on.

I query to get all the favouites, then i output those favourites down the page

each favourite has a "remove from favourites" link - i want to make it so when a user clicks this link they get a javascript confirm box pop up saying "are you sure you want to remove #favourites name# from your favourites?"

so i have..

function remove(profile,name) {
if (confirm("Are you sure you want to remove #name# from your favourites?")) {
window.location = "remove_from_favourites.cfm?deleteFavID=#profile#&removedName=#name#"
}
}

and

<div onClick="remove(#rs_favs.favouriteeID#,#rs_favs.firstName#)" >Remove from favourites</div>

but it doesn't work - i think there are 2 problems..

problem 1 - i have used the CF # in the remove function as i don't know what one does in javascript to "output" a variable (see where it says "Are you sure you want to remove #name# from your favourites") what should i use in place of the "#"?

problem 2 - i have tried to pass to variables to the function >> onClick="remove(#rs_favs.favouriteeID#,#rs_favs.firstName#)" and i have separated them but a commer "," but i suspect that is incorrect syntax


if anyone could please help me i'd be very grateful as, to be honest, i love coldfusion - but javascript just hurts my head! :(

thanks very much indeed.

PS the way i've wrapped my "remove from favourites" text in a div rather than making it an actual "a href" link - is that right or is there a better way (ideally i'd like the text to look like all my other links (ie they underline on mouse over)
This topic is closed to new replies. Start a new post to keep the conversation going.
CFDEBUGCorrect answer
Inspiring
July 19, 2006
thanks very much CFDEBUG

that seems to be a step in the right direction - but now i'm getting a javascript error " 'Richard' is undefined " (when i click to remove richard from my favourties - so the string "Richard" must be being successfully passed to the funciton by the onclick command so what does it mean it's undefined??

any further help very much appreciated - thanks


That's because you are passing a string value without single quotes in your onclick() function.

Change it to

<div onClick="remove(#rs_favs.favouriteeID#,'#rs_favs.firstName#')" >Remove from favourites</div>

10 replies

Nick WayAuthor
Inspiring
July 20, 2006
thank you CFDEBUG and Ian - I ended up using the <a href="#"... solution as I wanted the underlining to dissapear on mouse over etc and it just seemed neater to make the link a link (if you see what i mean)

so now my page is all done and live - thanks guys i really appreciate your help, thank you :D

kind regards
happysailingdude
Translate
Inspiring
July 20, 2006
You can simply add CSS commands to define your div to look like your links.

div#FavLink
{
color: blue;
text-decoration: underline;
cursor: pointer;
}

...

<div id="FavLink"
onClick="remove(#rs_favs.favouriteeID#,'#rs_favs.firstName#')" Remove
from favourites</div>

That should get you started.



happysailingdude wrote:
> thanks CFDEBUG that sorted it - thanks very much
>
> just one last thing - more of a CSS issue i think but maybe not.
>
> so now I have <div
> onClick="remove(#rs_favs.favouriteeID#,'#rs_favs.firstName#')" >Remove from
> favourites</div>
>
> which of course isnt a link so it just looks like my standard text (as if i'd
> done <p>Remove from favourites</p>
>
> how do i make the "Remove from favourites" text look like a link as if i'd
> done <a href="remove_from_favourites.cfm?....." >Remove from favourites</a>
>
> i think ive seen links that when you hover over them it says "javascript:void"
> or something at the bottom of the browser - is this the answer and if so what
> is the correct syntax please? or do i need to do something with my CSS file?
>
> thanks very much indeed for any further help anyone can give me to get this
> page finshed off - thank you
>
Translate
Nick WayAuthor
Inspiring
July 20, 2006
thanks CFDEBUG that sorted it - thanks very much

just one last thing - more of a CSS issue i think but maybe not.

so now I have <div onClick="remove(#rs_favs.favouriteeID#,'#rs_favs.firstName#')" >Remove from favourites</div>

which of course isnt a link so it just looks like my standard text (as if i'd done <p>Remove from favourites</p>

how do i make the "Remove from favourites" text look like a link as if i'd done <a href="remove_from_favourites.cfm?....." >Remove from favourites</a>

i think ive seen links that when you hover over them it says "javascript:void" or something at the bottom of the browser - is this the answer and if so what is the correct syntax please? or do i need to do something with my CSS file?

thanks very much indeed for any further help anyone can give me to get this page finshed off - thank you
Translate
Inspiring
July 20, 2006
Just do
<a href="#" onClick="remove(<cfoutput>#rs_favs.favouriteeID#,'#rs_favs.firstName#'</cfoutput>)">Remove from favorites</a>

And also there are several sites like irt.org, javascript.com you can refer for javascript questions. You may be able to find answers to pretty much all kinds of js questions.

Edit: Use Skinner's suggestion. I didn't read your question correctly. You could use his suggestion and keep your div tag or you could use mine but have to remove the div tag. Either way will work
Translate
Inspiring
July 19, 2006
you can put <cfoutput> tags inside javasript.

<div onClick="remove(<cfoutput>#rs_favs.favouriteeID#,#rs_favs.firstName#</cfoutput>)" >

function remove(profile,name) {
if (confirm("Are you sure you want to remove<cfoutput> #name#</cfoutput> from your favourites?")) {
window.location = "remove_from_favourites.cfm?<cfoutput>deleteFavID=#profile#&removedName=#name#</cfoutput>"
}
}
Translate
Nick WayAuthor
Inspiring
July 19, 2006
thanks for your reply but alas that isn't the problem - i have already got my cfoutput tags in place

the problem is that profile & name are javascript variables (passed when the "remove" function is called by the "onclick" command)

see "function remove(profile,name) {...." i am trying to "output" these variables within the javascript

maybe i'm not explaining myself very well - sorry

does anyone get what i'm on about?

thanks very much

Translate
Inspiring
July 19, 2006
Oh then you need to use javascript concat operator

function remove(profile,name) {
if (confirm("Are you sure you want to remove " +name +" from your favourites?")) {
window.location = "remove_from_favourites.cfm?deleteFavID=" + profile + "&removedName=" +name
}
}
Translate