<a href="#" onclick="document.forms['printletter0'].submit();">Print</a>

Well, this is not working, not getting why.

<a href="#" onclick="document.forms['printletter0'].submit();" class="box">Imprimer</a>

The form is something like -

<form name="printletter0" id="printletter0" method="post" action="mypage.php">
<input type="hidden" name="id" value="431" />
<input type="hidden" name="mytext" value="My text goes here" />
Some contents here
<div style="padding-top:6px;">
  <img align="top" style="padding-top:0px;" src="img/printer.gif" height="15" width="20" border="0" hspace="0" vspace="0" alt="" />&nbsp;
    <a href="#" onclick="document.forms['printletter0'].submit();" class="box">Print</a>

Any suggestion guys? Thanks.

error in IE

document.forms.printletter0 is null or not an object

error in FF

document.forms.printletter0 has no proerties

That’s because the forms array is accessed by an index (a number) as explained here.

The form doesn’t need a name attribute and you can access it like this:

<a href="#" onclick="document.getElementById('printletter0').submit();" class="box">Imprimer</a>

or like this:

<a href="#" onclick="this.parentNode.parentNode.submit();" class="box">Imprimer</a>

However, you should consider using a normal submit button for this, since submitting the form with javascript appears to have no added benefits:

<input type="submit" value="Imprimer" />

Also, unless you’re using a Transitional doctype, forms are only allowed to contain block-level elements.

Hi Raffles

Thanks buddy. Well, using the first one,

<a href="#" onclick="document.getElementById('printletter<?=$i?>').submit();" class="box">Imprimer</a>

i’m getting in IE, Object doesn’t support this property or method. I’ve removed the name attribute from form.

FF tells me document.getElementById(“printletter0”) has no properties

Basically I am making a list of users in a page and a print command with each members, that will print the detailed profile of that member, that’s why I’m using javascript, not a simple submit button. I will have too many submit buttons in the page otherwise :smiley:

And yes, that’s why I avoid to use doctype strict. Transitional is more flexible.

Try the second one - that one should definitely work. Or, this would work too:

<a href="#" onclick="document.forms[<?=$i?>].submit();" class="box">Imprimer</a>

Though I prefer the second one from up there. Also, if you use the submit button, there will be no difference really - all you’re doing is changing an “Imprimer” link for an “Imprimer” button!

Thanks again. The first one, by parentnode, didn’t work, gave me the same error.

However, this one worked properly. by forms.

Thanks again. :slight_smile:

Yeah, thats what I was trying to avoid, to have 30 submit buttons in a page :smiley: