|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
This is what I came up with: Code:
HTMLSelectElement.prototype.selectedValue = function () {
return this.options[this.selectedIndex].value
}
I realize that I could implement this as a simple function call: Code:
function selectedValue(selBox) {
return selBox.options[selBox.selectedIndex].value;
}
![]() 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 |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
I was afraid of that.
![]() Thanks for the answer ![]() -Jeremy |
|
#4
|
||||
|
||||
|
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>
Code:
var txt = document.getElementById('selBox').options[document.getElementById('selBox').selectedIndex].value
Code:
var txt = document.getElementById('selBox').value
__________________
My Threads: Random daily advice thread | Prototype Function Collection | Function Library XMLHttpRequest: Specification | xmlhttp object | open and onreadystatechange order | String.prototype.toXMLDocument | Sarissa | Prototype | Dojo Broadening one's horizons: 24ways.org |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
||||
|
||||
|
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.
__________________
My Threads: Random daily advice thread | Prototype Function Collection | Function Library XMLHttpRequest: Specification | xmlhttp object | open and onreadystatechange order | String.prototype.toXMLDocument | Sarissa | Prototype | Dojo Broadening one's horizons: 24ways.org |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|