Help with multiple onclick=

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
Klown
Posts: 90
Joined: Tue Feb 22, 2011 5:59 am

Help with multiple onclick=

Post by Klown »

Hi Guys,

I'm toying with an idea and need help implementing it. The code below shows what I have already tried. I am trying to use the onclick event to confirm the action, upon confirmation navigate to the new page. The confirmation will popup and no matter the selection, it will not allow the execution location change. Any suggestions without using a hyperlink? I'm sure there is an easy solution for someone out there!

Code: Select all

<tr class="odd" onclick="return confirm(\'Build a '.$value.' for $'.number_format($value).'?\'); location.href=\'?cmd=build&id=' . $value . '&x=' . $value . '&y=' . $value . '\'"><td>
Thanks in advance.
if( $myGames != "Crap" ) {
  • please_donate( $money );
} else {
  • just_enjoy();
}
Mardonis
Posts: 139
Joined: Wed Jun 29, 2011 7:54 pm

Re: Help with multiple onclick=

Post by Mardonis »

I found this:

Code: Select all

$("#unassignAll").on('click', function () {
    $(".unassignButton").trigger('click');
});
From here - http://stackoverflow.com/questions/1541 ... -href-link

Says it will call the click callback bound to all of the unassign buttons simultaneously. is this what you are somewhat looking for?
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Help with multiple onclick=

Post by hallsofvallhalla »

little confusing on what you are trying to accomplish. Sure there is not another way to look at it?

Also use \" vs \'
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Help with multiple onclick=

Post by Jackolantern »

You are returning the result of the confirm as the last action in the event. So all you are doing is returning either true or false, which won't do anything since you are clicking a <tr> element which has no default browser click action.

Also, if you plan to have a lot of JS on your page, you will regret adding the code onto the DOM, as it will become a mess that is very hard to maintain or browse later.

Lets say that you add an id of "t" to the element. Here is how to do it with jQuery:

Code: Select all

$('#t').click(function(e){
    if (confirm('same text as your example')) {
          window.location = "www.newpage.com/script.php";
    }
});
And without jQuery, although note you can only have one function attached at a time this way without jQuery. The solution without jQuery to attach multiple event handlers is much uglier:

Code: Select all

document.getElementById('t').onclick=function(){
     if (confirm('same text as your example')) {
          window.location = "www.newpage.com/script.php";
    }
};
The indelible lord of tl;dr
Klown
Posts: 90
Joined: Tue Feb 22, 2011 5:59 am

Re: Help with multiple onclick=

Post by Klown »

Sorry for the very late reply I wanted to update you and let you know this did in fact work for my situation -I modified your suggestion to work inline so I could create it dynamically.

You're a true pro - and life saver!

Thanks for all the help.

Code: Select all

     if (confirm('Confirmation Message Here?')) {
          window.location = "www.newpage.com/script.php";
    }
if( $myGames != "Crap" ) {
  • please_donate( $money );
} else {
  • just_enjoy();
}
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Help with multiple onclick=

Post by Jackolantern »

You're most welcome :)
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”