Is there any way to submit the current form in which i have the href link like this
<a href="javascript:;" onclick="this.submit();">Save</a>
I want to use same code evrywhere so i can't use any id or class
Is there any way to submit the current form in which i have the href link like this
<a href="javascript:;" onclick="this.submit();">Save</a>
I want to use same code evrywhere so i can't use any id or class
Submit form using this.form.submit()
i.e in your case it will be like
<a href="#" onclick="this.form.submit();">Save</a>
But it highly recommended to use form name
otherwise if you are comfortable using jquery you can also use jquery closest function
$(field).closest("form").submit();
What you want is:
<a href="javascript:;"onclick="document.forms.<formname>.submit();">Save</a>
Actually it seems to work when <formname>
is replaced with the form's id also.
Althoughy I would strongly recommend you research the practice known as Unobtrusive Javascript
<a href="" onclick="!function(a){while(a&&a.nodeName!="FORM")a=a.parentNode;if(a)a.submit();}(this);"></a>
<a href="" onclick="!function(a){while(a&&a.nodeName!='FORM')a=a.parentNode;if(a)a.submit();}(this);"></a>
. –
Abysm I use this solution for unknown form name:
function submitForm(originator)
{
var pN=originator.parentNode;
while (true)
{
if (pN&&pN.nodeName=='FORM')
{
pN.submit();
break;
}
pN=pN.parentNode;
}
}
and now on any document's object event we can use this function:
<div onclick="doSomething();submitForm(this)">test</div>
Say you have n
number of forms on your page. You can submit i+1
th form by using:
document.forms[i].submit();
So if you have just one, and want to submit on link click,
document.forms[0].submit();
should be your onclick
attribute's value
You can try this code
<a href="" onkeypress="submit(this.form)"/>
<script>
function submit(form) {
form.submit();
}
</script>
I use more tricky way.
make your button looks like <a href>
<button type="submit" style="background-color: transparent!important;border: none;padding: 0!important;color: #3c8dbc;cursor: pointer;box-sizing: border-box;">MY LINK BUTTON</button>
© 2022 - 2024 — McMap. All rights reserved.