SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting an element of a form without it being named

    The problem I'm facing is I have a form which isn't name (and no I can't name it either ). And I want to fill in the value of one of the text inputs.

    How do I access an element from an un-named form.

    Cheers,
    - Dean

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,426
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Assuming it is the only form on the page (or the first one):

    Code:
    document.forms[0].elements[x]
    The 'x' is the number of the element in the form. So if it a text field and it is the 10th element, 'x' would be '9'.

    Or use:

    Code:
    document.getElementById('name')
    This is assuming the element has an id assigned to it.

  3. #3
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tips but I'm not getting it to work. You see, I'm trying to make a favelet which fills in an un-named form element. Take a look at my website for example:

    http://www.deanclatworthy.com/contact/

    I wrote this favelet:

    Code:
    javascript: document.form[0].elements[name].value = 'moo'; void(NULL);
    And it's not filling in the form. Can you see where i'm going wrong ?

  4. #4
    SitePoint Evangelist
    Join Date
    Sep 2003
    Location
    San Diego, CA
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe it would be...
    Code:
    elements['name']
    ...you forgot the quotes...you don't need quotes if it is a number though...
    Edit:


    This, of course, is assuming you have an element named 'name' in the first form on the page...also, does using the name 'name' affect any scripts in javascript if trying to refer to it?

  5. #5
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,426
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Code:
    java_script_: document.form[0].elements[name].value = 'moo'; void(NULL);
    Not name but number

    Code:
    java_script_: document.form[0].elements[4].value = 'moo'; void(NULL);
    The above assumes that the text field you want to alter is the fifth element in the form.

  6. #6
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just tried it and it didn't work unfortunately And yes I did change the number to 0 and 1 and 2 and 3 to see if it placed it in any of the fields

  7. #7
    SitePoint Evangelist
    Join Date
    Sep 2003
    Location
    San Diego, CA
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you getting any errors?

  8. #8
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Error: document.form has no properties
    Source File: javascript: document.form[0].elements[2].value = 'moo'; void(NULL);
    Line: 1
    Just had to open the firefox javascript console

  9. #9
    SitePoint Evangelist
    Join Date
    Sep 2003
    Location
    San Diego, CA
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this...
    Code:
    java_script_: document.forms[0].elements[2].value = 'moo'; void(NULL);
    But you can chagne the 2 to whatever it needs to be, 2 might work...notice the s in forms, you had form before!

  10. #10
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's this?
    Code:
    void(NULL);
    ::: certified wild guess :::

  11. #11
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It stops the favelet doing anything after all your processing I believe That did the track adios - a measly typo by me

    Thanks for your help,
    Cheers,
    - Dean

  12. #12
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The 'void' was OK - but the argument wasn't. null is all-lowercase in JS. Use a simple zero instead (void(0)).
    ::: certified wild guess :::

  13. #13
    SitePoint Evangelist
    Join Date
    Sep 2003
    Location
    San Diego, CA
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    removing void(NULL); worked? Can I see your finished code?

  14. #14
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh sorry one last question. How do I then submit the form of the page that I'm on ?

  15. #15
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you post a more complete example of how this is all being managed? The url you gave doesn't really show much....
    ::: certified wild guess :::

  16. #16
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry let me post my code then:

    javascript:

    document.location = 'http://www.deanclatworthy.com/contact';
    name2=prompt('Name here','');
    email2=prompt('Email here','');
    content2=prompt('Content here','');


    document.forms[0].elements[1].value = name2;
    document.forms[0].elements[3].value = email2;
    document.forms[0].elements[4].value = content2;
    If you add a bookmark with that content (make sure you put it all on one line) and then go to it you'll see what it does. All I need to do now is make the form submit

  17. #17
    SitePoint Evangelist
    Join Date
    Sep 2003
    Location
    San Diego, CA
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just add....
    Code:
    document.forms[0].submit();

  18. #18
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Error: document.forms[0].submit is not a function
    Source File: javascript:document.location = 'http://www.deanclatworthy.com/contact';name2=prompt('Name here','');email2=prompt('Email here','');content2=prompt('Content here','');document.forms[0].elements[1].value = name2;document.forms[0].elements[3].value = email2;document.forms[0].elements[4].value = content2;document.forms[0].submit();
    Line: 1
    That's the error this time

  19. #19
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming there is a form in there somewhere the only likely cause for this is a submit button that you've named "submit". If that's the case, use another name. Still don't follow your code (above). btw it's window.location.

    Edit:

    OK, don't feel bad, this pops up once-a-month, like clockwork...<input type="submit" name="submit" value="Send" />...as I said btw, you'll get better feedback if you format your HTML so it's readable...almost dislocated my elbow finding this. And: it's bookmarklet. cheers !
    Last edited by adios; Jul 7, 2004 at 19:43.
    ::: certified wild guess :::

  20. #20
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a note - my HTML code is generate via a template system. It's the parsing of it which isn't nicely formatted I have it set to trim whitespace to save bandwidth.

  21. #21
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The thing is most <input type="submit" /> are named submit - so this is where the stumbling block falls in my code I guess

  22. #22
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So...what's going on out there in Fave-land? Are we happenin'?

    Most HTML submit buttons might be named 'submit' - too busy to check - but most HTML forms are submitted by - HTML (not JS) so, overwriting the Form.submit() method goes unnoticed. I have no idea why browser developers haven't fixed this, it's an obvious pitfall.
    ::: certified wild guess :::

  23. #23
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unfortunately the name="submit" was the final hurdle that I fell at with this favelet. It's not possible to overcome it I guess so we have a non-working favelet I'm afraid - ah well it was a nice idea

  24. #24
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    javascript:self.location='http://www.deanclatworthy.com/contact';name2=prompt('Name here','name');email2=prompt('Email here','email');content2=prompt('Content here','content');f=document.forms[0].elements;f[1].value=name2;f[3].value=email2;f[4].value=content2;f.submit.click();void(0);
    Or for that url you posted:
    Code:
    javascript&#58;self.location='http://www.deanclatworthy.com/contact';els=document.forms[0].elements;els['name'].value=prompt('Name here','name');els['email'].value=prompt('Email here','email');els['content'].value=prompt('Content here','content');els.submit.click();void(0);
    Last edited by adios; Jul 11, 2004 at 18:50.
    ::: certified wild guess :::


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
  •