7 Replies - 411 Views - Last Post: 27 February 2014 - 11:17 AM

#1 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 433
  • View blog
  • Posts: 2,998
  • Joined: 12-January 10

quick var question

Posted 27 February 2014 - 10:19 AM

so I would like to know why the following returns the index but the bottom one returns undefined

alert(document.Form1.elements["PS_FORM/SUBJECT_PROPERTY/Repair_Type"+i].selectedIndex);



var selected_index = (document.Form1.elements["PS_FORM/SUBJECT_PROPERTY/Repair_Type"+i].selectedIndex);





in this context
for(var i = 1; i<6; i++){
           var selected_index = (document.Form1.elements["PS_FORM/SUBJECT_PROPERTY/Repair_Type"+i].selectedIndex);

alert(document.Form1.elements["PS_FORM/SUBJECT_PROPERTY/Repair_Type"+i].selectedIndex);

alert(selected_index.selected);
}


This post has been edited by DarenR: 27 February 2014 - 10:28 AM


Is This A Good Question/Topic? 0
  • +

Replies To: quick var question

#2 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 325
  • View blog
  • Posts: 1,198
  • Joined: 15-January 14

Re: quick var question

Posted 27 February 2014 - 10:24 AM

Based on only those lines without any other context, I would guess it has something to do with the variable i not being set to the same thing.
Was This Post Helpful? 0
  • +
  • -

#3 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 433
  • View blog
  • Posts: 2,998
  • Joined: 12-January 10

Re: quick var question

Posted 27 February 2014 - 10:36 AM

I updated my question.
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 325
  • View blog
  • Posts: 1,198
  • Joined: 15-January 14

Re: quick var question

Posted 27 February 2014 - 10:51 AM

The second alert would be undefined because selectedIndex does not have a selected property. If you alerted selected_index instead of selected_index.selected you should see the same thing.
Was This Post Helpful? 0
  • +
  • -

#5 FerretHolmes  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 167
  • Joined: 12-November 12

Re: quick var question

Posted 27 February 2014 - 10:57 AM

Out of curiosity, can you reference a DOM form by simply using it's name? I thought you had to reference it by using an array index such as document.forms[0]?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3214
  • View blog
  • Posts: 10,777
  • Joined: 12-December 12

Re: quick var question

Posted 27 February 2014 - 11:05 AM

All of the following produce the output 2:

<form id="formsid" name="formsname">
    <input type="text">
    <input type="submit">
</form>
<script>
    console.log(document.forms[0].length);
    console.log(document.forms.formsid.length);
    console.log(document.forms.formsname.length);
    console.log(formsname.length);
    console.log(formsid.length);
</script>

even in IE.

We should no longer use the name-attribute for a form though, using an id instead.

This post has been edited by andrewsw: 27 February 2014 - 11:09 AM

Was This Post Helpful? 1
  • +
  • -

#7 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 325
  • View blog
  • Posts: 1,198
  • Joined: 15-January 14

Re: quick var question

Posted 27 February 2014 - 11:09 AM

In HTML5 at least, the form element does in fact have a getter defined for a name:

http://www.whatwg.or...he-form-element

I don't see anything like that on generic element nodes, but the form element nodes at least have it explicitly defined.
Was This Post Helpful? 0
  • +
  • -

#8 FerretHolmes  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 167
  • Joined: 12-November 12

Re: quick var question

Posted 27 February 2014 - 11:17 AM

I see. Thanks for doing the tests for me, I am on a tablet and didn't want to try and code :whistling:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1