www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 11-04-2005, 07:27 PM
    kitchen kitchen is offline
    Registered User
     
    Join Date: Nov 2005
    Posts: 2
    select box selectedValue() method

    Hi folks,

    I'm sick of typing stuff like:

    Code:
    var txt = document.getElementById('selBox').options[document.getElementById('selBox').selectedIndex].value
    so I sought out to add a method to the HTMLSelectElement class called selectedValue.

    This is what I came up with:
    Code:
    HTMLSelectElement.prototype.selectedValue = function () {
       return this.options[this.selectedIndex].value
    }
    which works wonderfully in Firefox, but not in IE or konqueror. Is this valid and IE/konq are non-compliant, or is FF being nice and going the extra mile to let me do it?

    I realize that I could implement this as a simple function call:
    Code:
    function selectedValue(selBox) {
      return selBox.options[selBox.selectedIndex].value;
    }
    and then retrieve the value with selectedValue(document.getElementById('selBox')), but I thought it would be more fun to implement as a method to the existing object

    So, long story short, is it valid to extend the existing HTMLSelectElement class with my own methods or should I just stick to using functions?

    I'm not concerned with browser support of this, I would like to know if it's *valid* ... so I know who to point the finger at

    -Jeremy
    Reply With Quote
      #2  
    Old 11-04-2005, 11:31 PM
    Kravvitz Kravvitz is offline
    CSS & JS/DOM Adept
     
    Join Date: Jul 2005
    Location: USA
    Posts: 3,914
    Unfortunately, not all browsers expose the HTML*Element objects in such a way that you can add prototypes.

    Since the custom methods don't work cross-browser, I recommend just sticking with normal functions.
    __________________
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions

    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

    Check out my blog.
    Reply With Quote
      #3  
    Old 11-06-2005, 04:14 AM
    kitchen kitchen is offline
    Registered User
     
    Join Date: Nov 2005
    Posts: 2
    I was afraid of that.

    Thanks for the answer

    -Jeremy
    Reply With Quote
      #4  
    Old 11-06-2005, 10:17 AM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,828
    Something like this will do what you want.
    Code:
    <script type="text/javascript">
    onload=function(){
    var els=document.getElementsByTagName("select"),i,l=els.length;
    
    for(i=0;i<l;i++){
    els[i].selectedValue = function(){return this.options[this.selectedIndex].value};
    els[i].selectedText = function(){return this.options[this.selectedIndex].text};
    }
    
    }
    </script>
    BTW, instead of doing:
    Code:
    var txt = document.getElementById('selBox').options[document.getElementById('selBox').selectedIndex].value
    You can also write it like this:
    Code:
    var txt = document.getElementById('selBox').value
    Reply With Quote
      #5  
    Old 11-06-2005, 07:37 PM
    Kravvitz Kravvitz is offline
    CSS & JS/DOM Adept
     
    Join Date: Jul 2005
    Location: USA
    Posts: 3,914
    That's a good idea, Ultimater.

    NS4.x doesn't support the value property of select elements, which is why the other way is used. I don't know if any other JavaScript browsers don't support it.
    __________________
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions

    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

    Check out my blog.
    Reply With Quote
      #6  
    Old 11-06-2005, 07:55 PM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,828
    Ohhhh, that is the reason why I see people writting it the long way, thanks for the information Kravvitz.

    Anyways it doesn't really make much of a differenece in NS4.x 's case anyways because it lacks DOM support. e.g. the getElementsByTagName DOM method would be a good example of an irreplaceable DOM method. However I'll know for the future.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:16 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.