View Full Version : A js Question
zlinuk
May 15, 2009, 09:28 AM
This is hopefully an easy one for those amongst you that are js guru's.
So here is the scenario;
I have a very simple form which contains a couple of text fields and a single button.
When the button is pressed, I want three things to happen;
1 The form data is submitted.
2 The form is reset.
3 A hidden link is "clicked"
So, I created (copied from internet) the following function;
<script type="text/javascript">
function submit_form()
{
document.form1.submit();
document.form1.reset();
document.getElementById("mb2").click();
}
</script>
The function is called when the button is clicked.
<input name="Button" type="button" class="submit1" id="submit1" onclick="submit_form()" value="Submit" />
The situation is that the submit and the reset works fine across the board, the ".click()" works in IE and throws an error in FF.
Can anyone offer a solution please?
angelwatt
May 15, 2009, 12:03 PM
See Mozilla's documentation for the click function. (https://developer.mozilla.org/En/DOM:element.click) You'll need to make sure you're applying the function to an appropriate element. You didn't supply all your code so I can't what it currently applies to.
zlinuk
May 15, 2009, 03:23 PM
Thanks for that, it seems that I can only use .click on form elements.
I'm actually trying to activate a lightbox from a hidden <a> tag.
The page can be found here http://www.exac-one.com/Contact/index.html (http://www.exac-one.com/Contact/index.html)
I accept that what i'm trying to do is a poor implementation, but my knowledge of javascript is virtually zero.
GregE
May 15, 2009, 03:34 PM
http://www.exac-one.com/Contact/index.html (http://http://www.exac-one.com/Contact/index.html)
Your link is bad. You've got an extra http:// in it.
effenay
May 16, 2009, 12:50 AM
Can anyone offer a solution please?
Try:
<script type="text/javascript">
function submit_form()
{
document.form1.submit();
document.form1.reset();
document.getElementById("mb2").click();
return false;
}
</script>
The additional line will prevent the default submit event from firing, which is probably preventing Firefox from completing the call to click().
effenay
May 16, 2009, 01:21 AM
Ah, forgetful me. The real problem is that Firefox does not support calling click() on <a> tags, as angelwatt pointed out above.
But, we can still make invoke your lightbox by calling it directly instead of trying to fake a click. Try this:
<script type="text/javascript">
var box = new MultiBox('mb', {descClassName: 'multiBoxDesc', useOverlay: true , fixedTop: 118 , showControls: false });
function submit_form() {
document.form1.submit();
document.form1.reset();
box.open(document.getElementById("mb2"));
return false;
}
</script>
BTW, I also noticed that you're referencing Spry.Widget.ValidationTextarea, but you've forgotten to link to SpryValidationTextarea.js
HTH.
dalvin200
May 16, 2009, 01:25 AM
you could also change the hidden <a> element to an <input button>..
here is the code which works in safari and firefox
<html>
<head>
<script type="text/javascript">
function submit_form()
{
document.form1.submit();
document.form1.reset();
document.getElementById("mb2").click();
}
function alertMsg()
{
alert("Hidden Click Worked");
}
</script>
<style type="text/css">
input.hidden{
display: none;
}
</style>
</head>
<body>
<form name="form1">
<input name="Button" type="button" class="submit1" id="submit1" onclick="submit_form()" value="Submit" />
<input type="button" name="mb2" id="mb2" value="" onclick="alertMsg()" class="hidden"/>
</form>
</body>
</html>
zlinuk
May 18, 2009, 06:24 PM
Thanks for all your help and suggestions.
I eventually went with this;
function submit_form()
{
document.form1.submit();
document.form1.reset();
if (navigator.appName=="Microsoft Internet Explorer")
document.getElementById("mb2").click();
else box.open(document.getElementById("mb2"));
}
which seems to work ok across the board.
vBulletin® v3.6.10, Copyright ©2000-2009, Jelsoft Enterprises Ltd.