Page 1 of 1

Help with multiple onclick=

Posted: Thu May 29, 2014 12:52 am
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.

Re: Help with multiple onclick=

Posted: Thu May 29, 2014 12:42 pm
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?

Re: Help with multiple onclick=

Posted: Thu May 29, 2014 2:30 pm
by hallsofvallhalla
little confusing on what you are trying to accomplish. Sure there is not another way to look at it?

Also use \" vs \'

Re: Help with multiple onclick=

Posted: Thu May 29, 2014 4:46 pm
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";
    }
};

Re: Help with multiple onclick=

Posted: Mon Jun 30, 2014 5:29 am
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";
    }

Re: Help with multiple onclick=

Posted: Mon Jun 30, 2014 9:02 pm
by Jackolantern
You're most welcome :)