Click to See Complete Forum and Search --> : Another Confirm question...


Nanwedar
06-15-2004, 10:29 AM
Hey all. I have the following:

<a href="delete.cfm" onclick="return confirm('Are you sure you want to delete this user?');">pic</a>

Even if I click Cancel on the confirmation popup, the form is being submitted.

I fixed this problem a while back by putting the confirm into the form's onSubmit handler, but I cannot do that in this case as there are too many other form elements that can also submit.

Anyone know how to solve this while keeping all the code inside the onclick handler?

Any insight greatly appreciated. Thanks!

Pittimann
06-15-2004, 11:02 AM
Hi!

I guess, we'll need some more code. The tag you posted can't submit a form like it is and clicking cancel correctly returns false.

Cheers - Pit

Nanwedar
06-15-2004, 11:29 AM
Ahh, yes. My bad.

The href tag actually has an appended variable on it..like

href='lock_item.cfm?lockStatus=1'

where lockStatus will be 1 or 0 depending on it's current state. Clicking the image the href is attached to toggles the item locked or unlocked.

So no it's not actually submitting, but rather firing the href location with variable appended. Therefore, I guess my real question is how can I build in a confirmation dialog that can cancel out the href redirect when cancel is selected on the confirm dialog.

Can an <a href="somewhere.html"> be controlled via javascript? Or will it redirect to that URL regardless of any onclick instructions?

Thanks!


EDIT-----

Got it doing what I need it to do with the following function called from the onclick.

function doUnlock(pageID)
{
var itemlocation = "view_topics.cfm?lockpage=" + pageID + "&pagelock=0";
var do_unlock = confirm("Do you really want to unlock this page?");
if (do_unlock == true)
{
window.location=itemlocation;
}
}

Seems like there should be a more elegant way to do this?

Pittimann
06-15-2004, 11:37 AM
Hi!

Clicking cancel in your confirm would return false! So I don't understand the problem.

The other thing is: in your onclick (or whatever), you can easily say onclick="this.href='#';"

Cheers - Pit