Help with function to dsable all buttons in HTML doccument

D

David Mark

It is so close to working now - I can't thank you enough for the time and
effort you have put in to helping me with this. Could I just ask one or two
more questions?

IE and FF are both telling me that I'm missing a closing parenthesis but I
can't work out where in { disableButtons(); oldOnClick(); }; })(o();); it
should be - can you help?

function oneClick() {
var buttons = document.getElementsByTagName("input");
for (var i = 0; i < buttons.length; i++) {
if (buttons.getAttribute("type") == "button" ||
buttons.getAttribute("type") == "submit") {
var oldOnClick = buttons.onclick;
buttons.onclick = (function(o) { return function(e)
{ disableButtons(); oldOnClick(); }; })(o(););


I should have made it more clear. Replace "oldOnClick()" with "o()",
not the passed parameter "o."

buttons.onclick = (function(o) { return function(e)
{ disableButtons(); o(e); }; })(oldOnClick);

And make sure oldOnClick actually exists before doing this.
 
J

James

I should have made it more clear. Replace "oldOnClick()" with "o()",
not the passed parameter "o."

buttons.onclick = (function(o) { return function(e)
{ disableButtons(); o(e); }; })(oldOnClick);

And make sure oldOnClick actually exists before doing this.


Thank you - that dealt with the javascipt errors. I seem to have introduced
a new (and hopefully, the last) problem where submit buttons (without
oldOnClick) do not disable before submitting. I guess this is due to the way
i am checking that oldOnClick exists - any ideas?

function oneClick() {
var buttons = document.getElementsByTagName("input");
for (var i = 0; i < buttons.length; i++) {
if (buttons.getAttribute("type") == "button" ||
buttons.getAttribute("type") == "submit") {
var oldOnClick = buttons.onclick;
if (oldOnClick) {
buttons.onclick = (function(o) { return function(e)
{ disableButtons(); o(e); }; })(oldOnClick);
} else {
buttons.onclick = (function(o) { return function(e)
{ disableButtons(); o(e); }; });
}
}
}
buttons = null;
}

Many thanks again
James
 
D

David Mark

I should have made it more clear. Replace "oldOnClick()" with "o()",
not the passed parameter "o."
buttons.onclick = (function(o) { return function(e)
{ disableButtons(); o(e); }; })(oldOnClick);

And make sure oldOnClick actually exists before doing this.

Thank you - that dealt with the javascipt errors. I seem to have introduced
a new (and hopefully, the last) problem where submit buttons (without
oldOnClick) do not disable before submitting. I guess this is due to the way
i am checking that oldOnClick exists - any ideas?

function oneClick() {
var buttons = document.getElementsByTagName("input");
for (var i = 0; i < buttons.length; i++) {
if (buttons.getAttribute("type") == "button" ||
buttons.getAttribute("type") == "submit") {
var oldOnClick = buttons.onclick;
if (oldOnClick) {
buttons.onclick = (function(o) { return function(e)
{ disableButtons(); o(e); }; })(oldOnClick);
} else {
buttons.onclick = (function(o) { return function(e)
{ disableButtons(); o(e); }; });


This is wrong. Change to:

buttons.onclick = disableButtons;
 
J

James

This is wrong. Change to:
buttons.onclick = disableButtons;


Thanks for that. Whilst it may well be right, IE still doesn't want to
submit the form afterwards:
http://www.graphics-line.co.uk/test/scriptstest.htm

I also tried:

buttons.onclick = function() {
disableButtons();
return true;
}

But this didn't work either:
http://www.graphics-line.co.uk/test2/scriptstest.htm

I wonder if I could somehow step back up through the node tree from the
submit button and call the forms submit method?

Many thanks
James
 
D

David Mark

This is wrong. Change to:
buttons.onclick = disableButtons;


Thanks for that. Whilst it may well be right, IE still doesn't want to
submit the form afterwards:http://www.graphics-line.co.uk/test/scriptstest.htm


That's because disableButtons disables all buttons, including the one
clicked. Pass the clicked button to it and skip that one.
I also tried:

buttons.onclick = function() {
disableButtons();
return true;
}

But this didn't work either:http://www.graphics-line.co.uk/test2/scriptstest.htm

I wonder if I could somehow step back up through the node tree from the
submit button and call the forms submit method?


You could and then you could disable the clicked button. Just use the
button's form property.

buttons.onclick = function() {
disableButtons();
this.form.submit();
}

And you have another problem. You fail to re-enable the buttons when
the user cancels the delete confirmation.
 
J

James

You could and then you could disable the clicked button. Just use the
button's form property.

buttons.onclick = function() {
disableButtons();
this.form.submit();
}


Yep, that did it!
And you have another problem. You fail to re-enable the buttons when
the user cancels the delete confirmation.

I never thought about that - thanks. Fixed quite simply with a
enableButtons() function.

It now works just as I want - many many thanks for your time, patience and
help.
James

For anyone who may be interested, the final script looks like:

window.onload = function() {
oneClick();
}

// Confirmation for delete buttons
function GoToDelete(url) {
var confirmresp = confirm('Are you sure?');
if (confirmresp) {
window.location = url;
} else {
enableButtons();
}
}

// Disable all buttons
function disableButtons() {
var buttons = document.getElementsByTagName("input");
for (var i=0; i < buttons.length; i++) {
if (buttons.getAttribute("type") == "button" ||
buttons.getAttribute("type") == "submit") {
buttons.disabled = true;
}
}
}

// Enable all buttons
function enableButtons() {
var buttons = document.getElementsByTagName("input");
for (var i=0; i < buttons.length; i++) {
if (buttons.getAttribute("type") == "button" ||
buttons.getAttribute("type") == "submit") {
buttons.disabled = false;
}
}
}

// Attach behavior to allow only a single click to all buttons
function oneClick() {
var buttons = document.getElementsByTagName("input");
for (var i = 0; i < buttons.length; i++) {
if (buttons.getAttribute("type") == "button" ||
buttons.getAttribute("type") == "submit") {
var oldOnClick = buttons.onclick;
if (oldOnClick) {
buttons.onclick = (function(o) { return function(e)
{ disableButtons(); o(e); }; })(oldOnClick);
} else if (buttons.form) {
buttons.onclick = function() {
disableButtons();
this.form.submit();
}
}
}
}
buttons = null;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,154
Messages
2,570,870
Members
47,400
Latest member
FloridaFvt

Latest Threads

Top