|
|
|
date: Wed, 3 May 2006 10:11:38 -0700,
group: microsoft.public.inetsdk.programming.scripting.jscript
back
Re: function question
Jeremy Chapman <please@Idontlikespam> wrote:
> I have a span object, and inside the span I have an input of type
> text. With javascript I can set a and get the contents of the input
> control by using it's value property. I have used javascript to add
> a function to the span object which returns the value of the input
> control (example below).
> This works well if I call it as a function, but I want to use it as a
> property so I can set and get the value, can this be done?.
>
> <html>
> <body>
> <span id="myspan">
> <input name="myinput" type="text" />
> </span>
>
> <script type="text/javascript">
> <!--
> var myspan = document.all ? document.all["myspan"] :
> document.getElementById("myspan");
> var myinput = document.all ? document.all["myspan"] :
> document.getElementById("myinput");
> myspan.value = function(){return myinput.value;} //<-- how can I
> change this to work as a getter and a setter?
In general, you can't. JavaScript does not support the notion of getters
and setters.
There are solutions specific to FireFox and to IE. In FireFox, you can
say
myspan.__defineGetter__("value", function() {return myinput.value;});
myspan.__defineSetter__("value", function(newVal) {myinput.value =
newVal;});
In IE, you can define a behavior and attach it to the element: see
http://msdn.microsoft.com/workshop/author/behaviors/overview.asp
Probably more trouble than it's worth.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
date: Wed, 3 May 2006 13:30:52 -0400
author: Igor Tandetnik
Re: function question
Jeremy Chapman <please@Idontlikespam> wrote:
> I have a span object, and inside the span I have an input of type
> text. With javascript I can set a and get the contents of the input
> control by using it's value property. I have used javascript to add
> a function to the span object which returns the value of the input
> control (example below).
> This works well if I call it as a function, but I want to use it as a
> property so I can set and get the value, can this be done?.
>
> <html>
> <body>
> <span id="myspan">
> <input name="myinput" type="text" />
> </span>
>
> <script type="text/javascript">
> <!--
> var myspan = document.all ? document.all["myspan"] :
> document.getElementById("myspan");
> var myinput = document.all ? document.all["myspan"] :
> document.getElementById("myinput");
> myspan.value = function(){return myinput.value;} //<-- how can I
> change this to work as a getter and a setter?
In general, you can't. JavaScript does not support the notion of getters
and setters.
There are solutions specific to FireFox and to IE. In FireFox, you can
say
myspan.__defineGetter__("value", function() {return myinput.value;});
myspan.__defineSetter__("value", function(newVal) {myinput.value =
newVal;});
In IE, you can define a behavior and attach it to the element: see
http://msdn.microsoft.com/workshop/author/behaviors/overview.asp
Probably more trouble than it's worth.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
date: Wed, 3 May 2006 13:30:52 -0400
author: Igor Tandetnik
Re: function question
Thanks for the info. I think I'll concdede defeat on this one. I have to
support IE, and behaviors aren't going to cut the mustard, because I want a
solution that is encapsulated entirely in javascript.
"Igor Tandetnik" wrote in message
news:u96PSdtbGHA.3856@TK2MSFTNGP03.phx.gbl...
> Jeremy Chapman <please@Idontlikespam> wrote:
>> I have a span object, and inside the span I have an input of type
>> text. With javascript I can set a and get the contents of the input
>> control by using it's value property. I have used javascript to add
>> a function to the span object which returns the value of the input
>> control (example below).
>> This works well if I call it as a function, but I want to use it as a
>> property so I can set and get the value, can this be done?.
>>
>> <html>
>> <body>
>> <span id="myspan">
>> <input name="myinput" type="text" />
>> </span>
>>
>> <script type="text/javascript">
>> <!--
>> var myspan = document.all ? document.all["myspan"] :
>> document.getElementById("myspan");
>> var myinput = document.all ? document.all["myspan"] :
>> document.getElementById("myinput");
>> myspan.value = function(){return myinput.value;} //<-- how can I
>> change this to work as a getter and a setter?
>
> In general, you can't. JavaScript does not support the notion of getters
> and setters.
>
> There are solutions specific to FireFox and to IE. In FireFox, you can say
>
> myspan.__defineGetter__("value", function() {return myinput.value;});
> myspan.__defineSetter__("value", function(newVal) {myinput.value =
> newVal;});
>
> In IE, you can define a behavior and attach it to the element: see
>
> http://msdn.microsoft.com/workshop/author/behaviors/overview.asp
>
> Probably more trouble than it's worth.
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>
>
date: Tue, 9 May 2006 09:11:52 -0700
author: Jeremy Chapman please@Idontlikespam
|
|