Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Identifying a line of script

  1. #1
    Join Date
    Apr 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Identifying a line of script

    var pick1 = parseInt(document.myForm.******.selectedIndex)==null?1arseInt(document.myForm.*****.selectedIndex);

    Could someone please help me by explaining how this line in a script works?

    Basically I have 2 lists that will be added together like dates.

    January in one list and 3rd in another form = January 3rd.

    Thanks!

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    I think that this is an if statement, a short one:
    Code:
    var * = ((condition) ? if its true : otherwise);
    I suggest making that line of code into this:
    Code:
    var pick1 = ((parseInt(document.myForm.******.selectedIndex)==null)? 1 : parseInt(document.myForm.*****.selectedIndex));
    Well, thats what I've been told.
    Hope this Helps,
    Nile
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    Softie2k (04-25-2008)

  4. #3
    Join Date
    Apr 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    thanks very much!

  5. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Quote Originally Posted by Softie2k View Post
    var pick1 = parseInt(document.myForm.******.selectedIndex)==null?1arseInt(document.myForm.*****.selectedIndex);
    Thanks!
    This is a ternary operation..a shorthand of the if statement

    Which means exactly the same as:
    Code:
    if (parseInt(document.myForm.******.selectedIndex)==null)
    {
    var pick 1 = 1;
    }
    else{
    pick1 = parseInt(document.myForm.******.selectedIndex);}
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  6. #5
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    He is very well right, except for the fact that he has a space in the first if(){ inbetween his pick and 1.

    Quote Originally Posted by rangana View Post
    This is a ternary operation..a shorthand of the if statement

    Which means exactly the same as:
    Code:
    if (parseInt(document.myForm.******.selectedIndex)==null)
    {
    var pick 1 = 1;
    }
    else{
    pick1 = parseInt(document.myForm.******.selectedIndex);}
    Last edited by Nile; 04-25-2008 at 04:39 AM.
    Jeremy | jfein.net

  7. The Following User Says Thank You to Nile For This Useful Post:

    rangana (04-25-2008)

  8. #6
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Yes Nile, you're right...you hit me on my head
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  9. #7
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Ah, anytime.
    Jeremy | jfein.net

  10. #8
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Any of the following will make the operation work.

    Code:
    var pick1 = document.myForm.******.selectedIndex == null ? 1 : document.myForm.*****.selectedIndex;
    
    var pick1 = (document.myForm.******.selectedIndex == null) ? 1 : document.myForm.*****.selectedIndex;
    It is not necessary to use parseInt as the selectedIndex used to return an index of the type number.

  11. #9
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Codeexploiter, you can also put the whole thing in a ( ), like this:
    Code:
    var pick1 = ((document.myForm.******.selectedIndex == null) ? 1 : document.myForm.*****.selectedIndex);
    Jeremy | jfein.net

  12. #10
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Yes of course but personally I feel that it is necessary especially if we are constructing a really complex operation using ternary operator. The one discussing here is a single operation equivalent to a simple if else condition.

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
  •