SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    Well, this is not working, not getting why.

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

    The form is something like -

    Code HTML4Strict:
    <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>
    </div>
    </form>

    Any suggestion guys? Thanks.

    error in IE

    Code:
    document.forms.printletter0 is null or not an object
    error in FF

    Code:
    document.forms.printletter0 has no proerties

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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:
    HTML Code:
    <a href="#" onclick="document.getElementById('printletter0').submit();" class="box">Imprimer</a>
    or like this:
    HTML Code:
    <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:
    HTML Code:
    <input type="submit" value="Imprimer" />
    Also, unless you're using a Transitional doctype, forms are only allowed to contain block-level elements.

  3. #3
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Raffles

    Thanks buddy. Well, using the first one,

    HTML Code:
    <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

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

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try the second one - that one should definitely work. Or, this would work too:
    HTML Code:
    <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!

  5. #5
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again. The first one, by parentnode, didn't work, gave me the same error.

    However, this one worked properly. by forms[].

    Thanks again.

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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •