Problem with form.elements.lenght

Discussion in 'Javascript' started by Pudlik, Szymon, Aug 3, 2005.

  1. Hi,

    I've written some code:


    function onSubmit(form){
    for (var i = 0; i < form.elements.lenght; i++){
    if (form.elements.disabled == 1)
    form.elements.disabled = 0;
    }
    }

    ....

    <form action="step3.jsp" method="get" name="myForm"
    onsubmit="onSubmit(this)">

    ....

    </form>


    I want to turn on all of <select> elements on my form by set their field
    'disabled' to false. Loop doesn't work. I've tried to get
    form.elements.lenght's value by putting it to alert box. IE (NS also)
    said "undefined". Where is the problem?

    Greetings, Szymon Pudlik.
     
    Pudlik, Szymon, Aug 3, 2005
    #1
    1. Advertising

  2. Pudlik, Szymon

    VK Guest

    Pudlik, Szymon wrote:
    > Hi,
    >
    > I've written some code:
    >
    >
    > function onSubmit(form){
    > for (var i = 0; i < form.elements.lenght; i++){
    > if (form.elements.disabled == 1)
    > form.elements.disabled = 0;
    > }
    > }
    >
    > ...
    >
    > <form action="step3.jsp" method="get" name="myForm"
    > onsubmit="onSubmit(this)">
    >
    > ...
    >
    > </form>
    >
    >
    > I want to turn on all of <select> elements on my form by set their field
    > 'disabled' to false. Loop doesn't work. I've tried to get
    > form.elements.lenght's value by putting it to alert box. IE (NS also)
    > said "undefined". Where is the problem?
    >
    > Greetings, Szymon Pudlik.



    document.forms[0].elements
     
    VK, Aug 3, 2005
    #2
    1. Advertising

  3. VK wrote:

    > document.forms[0].elements


    Hi,
    it is also not working. I've tried this before I've written on this group...

    Cheers, Szymon.
     
    Pudlik, Szymon, Aug 3, 2005
    #3
  4. On 03/08/2005 17:38, Pudlik, Szymon wrote:

    [snip]

    > if (form.elements.disabled == 1)
    > form.elements.disabled = 0;


    The disabled property is a boolean, not a number.

    [snip]

    > I want to turn on all of <select> elements on my form by set their
    > field 'disabled' to false.


    Your code would enable all form controls, not just SELECT elements.

    > I've tried to get form.elements.lenght's value by putting it to alert
    > box. IE (NS also) said "undefined". Where is the problem?


    You're spelling length incorrectly.

    function onSubmit(f) {
    var e = f.elements,
    i, n;

    for(i = 0, n = e.length; i < n; ++i) {
    if(/^select/.test(e.type)) {e.disabled = false;}

    /* ...or if('select-one' == e.type) {...} */
    }
    }

    Hope that helps,
    Mike

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
     
    Michael Winter, Aug 3, 2005
    #4
  5. Michael Winter wrote:

    > You're spelling length incorrectly.


    You are right. I've done a mistake.

    > function onSubmit(f) {
    > var e = f.elements,
    > i, n;
    >
    > for(i = 0, n = e.length; i < n; ++i) {
    > if(/^select/.test(e.type)) {e.disabled = false;}
    >
    > /* ...or if('select-one' == e.type) {...} */
    > }
    > }


    I've tried first option:

    function onSubmit(form){
    var el = form.elements;
    for (var i = 0; i < el.lenght; i++){
    if(/^select/.test(el.type)){
    el.disabled = false;
    }
    }
    }

    and second:

    function onSubmit(form){
    var el = form.elements;
    for (var i = 0; i < el.lenght; i++){
    if('select-one' == el.type){
    el.disabled = false;
    }
    }
    }

    and both doesn't work. All my select items are still disabled :(
    Is it possible that I do not understand what is "/^select/" and
    'select-one'.. Would you like to explain me that?

    Greetings, Szymon Pudlik.
     
    Pudlik, Szymon, Aug 3, 2005
    #5
  6. Michael Winter wrote:

    > You're spelling length incorrectly.


    You are right. I've done a mistake.

    > function onSubmit(f) {
    > var e = f.elements,
    > i, n;
    >
    > for(i = 0, n = e.length; i < n; ++i) {
    > if(/^select/.test(e.type)) {e.disabled = false;}
    >
    > /* ...or if('select-one' == e.type) {...} */
    > }
    > }


    I've tried first option:

    function onSubmit(form){
    var el = form.elements;
    for (var i = 0; i < el.lenght; i++){
    if(/^select/.test(el.type)){
    el.disabled = false;
    }
    }
    }

    and second:

    function onSubmit(form){
    var el = form.elements;
    for (var i = 0; i < el.lenght; i++){
    if('select-one' == el.type){
    el.disabled = false;
    }
    }
    }

    and both doesn't work. All my select items are still disabled :(
    Is it possible that I do not understand what is "/^select/" and
    'select-one'.. Would you like to explain me that?

    Greetings, Szymon Pudlik.
     
    Pudlik, Szymon, Aug 3, 2005
    #6
  7. Michael Winter wrote:

    > You're spelling length incorrectly.


    You are right. I've done a mistake.

    > function onSubmit(f) {
    > var e = f.elements,
    > i, n;
    >
    > for(i = 0, n = e.length; i < n; ++i) {
    > if(/^select/.test(e.type)) {e.disabled = false;}
    >
    > /* ...or if('select-one' == e.type) {...} */
    > }
    > }


    I've tried first option:

    function onSubmit(form){
    var el = form.elements;
    for (var i = 0; i < el.lenght; i++){
    if(/^select/.test(el.type)){
    el.disabled = false;
    }
    }
    }

    and second:

    function onSubmit(form){
    var el = form.elements;
    for (var i = 0; i < el.lenght; i++){
    if('select-one' == el.type){
    el.disabled = false;
    }
    }
    }

    and both doesn't work. All my select items are still disabled :(
    Is it possible that I do not understand what is "/^select/" and
    'select-one'.. Would you like to explain me that?

    Greetings, Szymon Pudlik.
     
    Pudlik, Szymon, Aug 3, 2005
    #7
  8. Pudlik, Szymon

    alu Guest

    "Pudlik, Szymon" <>
    > Hi,
    >
    > I've written some code:
    >
    >
    > function onSubmit(form){
    > for (var i = 0; i < form.elements.lenght; i++){
    > if (form.elements.disabled == 1)
    > form.elements.disabled = 0;
    > }
    > }
    >
    > ...
    >
    > <form action="step3.jsp" method="get" name="myForm"
    > onsubmit="onSubmit(this)">
    >
    > ...
    >
    > </form>
    >
    >
    > I want to turn on all of <select> elements on my form by set their field
    > 'disabled' to false. Loop doesn't work. I've tried to get
    > form.elements.lenght's value by putting it to alert box. IE (NS also)
    > said "undefined". Where is the problem?
    >
    > Greetings, Szymon Pudlik



    Spelling error. All instances of 'length' are spelled incorrectly.
    -alu
     
    alu, Aug 3, 2005
    #8
  9. Pudlik, Szymon

    Grant Wagner Guest

    "Pudlik, Szymon" <> wrote in message
    news:dcqrvp$thg0$...
    > Hi,
    >
    > I've written some code:
    >
    >
    > function onSubmit(form){
    > for (var i = 0; i < form.elements.lenght; i++){
    > if (form.elements.disabled == 1)
    > form.elements.disabled = 0;
    > }
    > }
    >
    > ...
    >
    > <form action="step3.jsp" method="get" name="myForm"
    > onsubmit="onSubmit(this)">
    >
    > ...
    >
    > </form>
    >
    >
    > I want to turn on all of <select> elements on my form by set their
    > field 'disabled' to false. Loop doesn't work. I've tried to get
    > form.elements.lenght's value by putting it to alert box. IE (NS also)
    > said "undefined". Where is the problem?


    length, not lenght.

    Also, the disabled property contains a boolean, not an number. Although
    type conversion will probably make the code work, it's best to stick to
    the type the property expects. As well, it appears you are testing to
    see whether every element is disabled, and if it is, you are enabling
    it. If all you want is to disable <select> elements, you should be
    testing for them before disabling every element on the page:

    function onSubmit(f) {
    f = f.elements;
    for (var i = 0; i < f.length; ++i) {
    if (f.type.indexOf('select') > -1 &&
    !f.disabled) {
    // if the element type is select-one
    // or select-multiple, and it's not currently
    // disabled, disable it
    f.disabled = true;
    }
    }
    }

    Be aware, if something goes wrong during the form submission, you have
    now left the user with a useless form because all the <select> elements
    are disabled.

    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
     
    Grant Wagner, Aug 3, 2005
    #9
  10. On 03/08/2005 18:27, Pudlik, Szymon wrote:

    > Michael Winter wrote:
    >
    >> You're spelling length incorrectly.

    >
    > You are right. I've done a mistake.


    And you're still doing it. There is no lenght property (anywhere in the
    DOM) so el.lenght evaluates to undefined. In relational comparisons such as

    i < el.lenght

    undefined operands cause the expression to evaluate to false. This means
    that your loop is never entered.

    Spell length properly as it is in my original code (or just use it
    verbatim); it ends 'th', not 'ht'.

    [snip]

    > Is it possible that I do not understand what is "/^select/" and
    > 'select-one'.. Would you like to explain me that?


    All form controls have a type property that reflects the type of
    control. SELECT elements have two possible values: select-one and
    select-multiple. When the boolean attribute, multiple, is added to a
    SELECT element, the type property will contain the latter (select-multiple).

    I assumed that you're using singular SELECT elements (rendered as a
    drop-down, not a list), so I suggested a direct test of 'select-one' as
    that should be quicker. But, just in case, the regular expression,

    /^select/

    could be used to check for either of the two values (it will match any
    string that begins [^] with the sequence, select).

    Mike

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
     
    Michael Winter, Aug 3, 2005
    #10
  11. Thanks for all answers.

    I do this like that:

    function onSubmit(form){
    var el = form.elements;
    for (var i = 0; i < el.length; i++){
    if('select-one' == el.type){
    el.disabled = false;
    }
    }

    }

    and it works :)

    Thanks.
    Szymon Pudlik.
     
    Pudlik, Szymon, Aug 4, 2005
    #11
  12. Pudlik, Szymon

    cosmic foo Guest

    "Pudlik, Szymon" <> wrote in message
    news:dcqrvp$thg0$...
    > Hi,
    >
    > I've written some code:
    >
    >
    > function onSubmit(form){
    > for (var i = 0; i < form.elements.lenght; i++){
    > if (form.elements.disabled == 1)
    > form.elements.disabled = 0;
    > }
    > }
    >
    > ...
    >
    > <form action="step3.jsp" method="get" name="myForm"
    > onsubmit="onSubmit(this)">
    >
    > ...
    >
    > </form>
    >
    >
    > I want to turn on all of <select> elements on my form by set their field
    > 'disabled' to false. Loop doesn't work. I've tried to get
    > form.elements.lenght's value by putting it to alert box. IE (NS also)
    > said "undefined". Where is the problem?
    >
    > Greetings, Szymon Pudlik.


    just use this instead,

    document.getElementById("yourformidhere").getElementsByTagName("*");
     
    cosmic foo, Aug 5, 2005
    #12
  13. Pudlik, Szymon

    Grant Wagner Guest

    "cosmic foo" <> wrote in message
    news:5wcIe.5504$...
    >
    > "Pudlik, Szymon" <> wrote in message
    > news:dcqrvp$thg0$...
    >> Hi,
    >>
    >> I've written some code:
    >>
    >>
    >> function onSubmit(form){
    >> for (var i = 0; i < form.elements.lenght; i++){
    >> if (form.elements.disabled == 1)
    >> form.elements.disabled = 0;
    >> }
    >> }
    >>
    >> ...
    >>
    >> <form action="step3.jsp" method="get" name="myForm"
    >> onsubmit="onSubmit(this)">
    >>
    >> ...
    >>
    >> </form>
    >>
    >>
    >> I want to turn on all of <select> elements on my form by set their
    >> field
    >> 'disabled' to false. Loop doesn't work. I've tried to get
    >> form.elements.lenght's value by putting it to alert box. IE (NS also)
    >> said "undefined". Where is the problem?
    >>
    >> Greetings, Szymon Pudlik.

    >
    > just use this instead,
    >
    > document.getElementById("yourformidhere").getElementsByTagName("*");


    Why not use document.forms[...].elements[...] when it is going to work
    in more browsers than your example, since it works in any browser that
    supports your example, as well as a collection of other browsers.

    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
     
    Grant Wagner, Aug 5, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vbMark
    Replies:
    3
    Views:
    402
    vbMark
    Jun 29, 2004
  2. TomislaW

    Repeater and Lenght ArrayList

    TomislaW, Dec 8, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    311
    Eliyahu Goldin
    Dec 8, 2004
  3. =?Utf-8?B?QVZM?=

    string.lenght vs string.empty

    =?Utf-8?B?QVZM?=, Apr 28, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    6,459
    Steve C. Orr [MVP, MCSD]
    Apr 29, 2005
  4. Replies:
    2
    Views:
    586
    Roedy Green
    Nov 27, 2005
  5. Kevin Auch

    Select.lenght problem

    Kevin Auch, May 18, 2004, in forum: Javascript
    Replies:
    2
    Views:
    118
    Kevin Auch
    May 18, 2004
Loading...

Share This Page