[jQuery] Re: val() and change()

2007-06-12 Thread Dan G. Switzer, II

Josh,

>Actually, there currently isn't a programmatic way to set the value
>and have it be masked automagically for the developer.  I just wanted
>to see if I could hook into existing methods to keep from having to
>add too much syntax.  It's driven off of focus,blur,keypress and
>pasting(on certain browsers).  I guess if a developer wants to mask
>their val they could call .val("something").blur() and everything
>would work out like I'm describing, it's just not as terse. :)
>
>Thanks for input.

What I'd do then, is to add a method like $.maskedVal() which calls the
internal val() and then runs whatever internal code of your that needs to
run to correctly format the input.

That way the developer just calls your special function to update the masked
value...

-Dan



[jQuery] Re: val() and change()

2007-06-12 Thread Josh Bush

Actually, there currently isn't a programmatic way to set the value
and have it be masked automagically for the developer.  I just wanted
to see if I could hook into existing methods to keep from having to
add too much syntax.  It's driven off of focus,blur,keypress and
pasting(on certain browsers).  I guess if a developer wants to mask
their val they could call .val("something").blur() and everything
would work out like I'm describing, it's just not as terse. :)

Thanks for input.

Josh

On Jun 12, 3:55 pm, "Dan G. Switzer, II" <[EMAIL PROTECTED]>
wrote:
> >That's kind of what I was thinking.  I was hoping that their might be
> >a better way.
>
> I'd advise against doing that by default. I suppose you could add it as
> config option for those users that might not have any control over how the
> field is being updated.
>
> I would think that would be exception though. I'd just expect people who are
> using the masked input plug-in to use your method for updating the field
> with a new value.
>
> -Dan



[jQuery] Re: val() and change()

2007-06-12 Thread Dan G. Switzer, II

>That's kind of what I was thinking.  I was hoping that their might be
>a better way.

I'd advise against doing that by default. I suppose you could add it as
config option for those users that might not have any control over how the
field is being updated. 

I would think that would be exception though. I'd just expect people who are
using the masked input plug-in to use your method for updating the field
with a new value.

-Dan



[jQuery] Re: val() and change()

2007-06-12 Thread Josh Bush

That's kind of what I was thinking.  I was hoping that their might be
a better way.

On Jun 12, 3:43 pm, "Dan G. Switzer, II" <[EMAIL PROTECTED]>
wrote:
> Josh,
>
> >My goal was to detect a val() call from within my masked input plugin.
>
> >So, when someone says "$("#id").maskedinput('(999) 999-')" , I
> >would love to be able to detect when that value had been set
> >programatically and then re-check the masking.
>
> The only way I know to do that, would be to continually poll the field to
> see if the value is something other than what you expected it to be.
> However, I don't think it's worth the overhead for the little return.
>
> -Dan



[jQuery] Re: val() and change()

2007-06-12 Thread Dan G. Switzer, II

Josh,

>My goal was to detect a val() call from within my masked input plugin.
>
>So, when someone says "$("#id").maskedinput('(999) 999-')" , I
>would love to be able to detect when that value had been set
>programatically and then re-check the masking.

The only way I know to do that, would be to continually poll the field to
see if the value is something other than what you expected it to be.
However, I don't think it's worth the overhead for the little return.

-Dan



[jQuery] Re: val() and change()

2007-06-12 Thread Klaus Hartl


Dan G. Switzer, II wrote:

Josh,


One more quick thing, is it possible to attach this to the val() of a
single input, or am I limited to extending this globally?


Instead of calling the below method "val" call it something else--like
"valChange()" or something. Then just call that new method any time you want
the change event to be triggered.

Also, since you're really only wanting to do this for a single field, you
could also just avoid the plug-in and just do: 


$("#myField").val("new value").trigger("change");

That would trigger the "change" event when you update the value.


$.fn.extend({
val: function( val ) {
return val == undefined ?
( this.length ? this[0].value : null ) :
this.attr( "value", val ).trigger("change");
}

});


And if you'd want a plugin there's not really a need to overwrite the 
existing method which a. duplicates code and b. can cause trouble 
somewhere else (I for example experienced some trouble recently because 
interface overwrites jQuery's own animate method which behaved 
differently then):


$.fn.extend({
valChange: function(v) {
return this.val(v).trigger('change');
}
});


--Klaus




[jQuery] Re: val() and change()

2007-06-12 Thread Josh Bush

My goal was to detect a val() call from within my masked input plugin.

So, when someone says "$("#id").maskedinput('(999) 999-')" , I
would love to be able to detect when that value had been set
programatically and then re-check the masking.

So, if someone does a "$("#id").val('555-867-5309')" later on, I could
reformat it properly to "(555) 867-5309".

Maybe that will shed some light on what my intentions are.

Thanks for your quick responses Dan!
Josh

On Jun 12, 3:30 pm, "Dan G. Switzer, II" <[EMAIL PROTECTED]>
wrote:
> Josh,
>
> >One more quick thing, is it possible to attach this to the val() of a
> >single input, or am I limited to extending this globally?
>
> Instead of calling the below method "val" call it something else--like
> "valChange()" or something. Then just call that new method any time you want
> the change event to be triggered.
>
> Also, since you're really only wanting to do this for a single field, you
> could also just avoid the plug-in and just do:
>
> $("#myField").val("new value").trigger("change");
>
> That would trigger the "change" event when you update the value.
>
> >> $.fn.extend({
> >> val: function( val ) {
> >> return val == undefined ?
> >> ( this.length ? this[0].value : null ) :
> >> this.attr( "value", val ).trigger("change");
> >> }
>
> >> });
>
> -Dan



[jQuery] Re: val() and change()

2007-06-12 Thread Dan G. Switzer, II

Josh,

>One more quick thing, is it possible to attach this to the val() of a
>single input, or am I limited to extending this globally?

Instead of calling the below method "val" call it something else--like
"valChange()" or something. Then just call that new method any time you want
the change event to be triggered.

Also, since you're really only wanting to do this for a single field, you
could also just avoid the plug-in and just do: 

$("#myField").val("new value").trigger("change");

That would trigger the "change" event when you update the value.

>> $.fn.extend({
>> val: function( val ) {
>> return val == undefined ?
>> ( this.length ? this[0].value : null ) :
>> this.attr( "value", val ).trigger("change");
>> }
>>
>> });

-Dan



[jQuery] Re: val() and change()

2007-06-12 Thread Dan G. Switzer, II

>Thank you. Any idea how I could attach to regular javascript call like
>" x.value='something' "?

You're not going to be able to do that in any kind of cross-browser way.
Just use a jQuery method/plug-in.

-Dan 



[jQuery] Re: val() and change()

2007-06-12 Thread Josh Bush

One more quick thing, is it possible to attach this to the val() of a
single input, or am I limited to extending this globally?

On Jun 12, 2:44 pm, "Dan G. Switzer, II" <[EMAIL PROTECTED]>
wrote:
> Josh,
>
> >I was just wondering, should a val() call invoke a change event?  If
> >not, is there a good way to emulate this behavior.
>
> You could overwrite the default behavior:
>
> $.fn.extend({
> val: function( val ) {
> return val == undefined ?
> ( this.length ? this[0].value : null ) :
> this.attr( "value", val ).trigger("change");
> }
>
> });
>
> I just append the .trigger("change") to the original code. This would fire
> off the onchange event after the value has been updated.
>
> -Dan



[jQuery] Re: val() and change()

2007-06-12 Thread Josh Bush

Thank you. Any idea how I could attach to regular javascript call like
" x.value='something' "?

On Jun 12, 2:44 pm, "Dan G. Switzer, II" <[EMAIL PROTECTED]>
wrote:
> Josh,
>
> >I was just wondering, should a val() call invoke a change event?  If
> >not, is there a good way to emulate this behavior.
>
> You could overwrite the default behavior:
>
> $.fn.extend({
> val: function( val ) {
> return val == undefined ?
> ( this.length ? this[0].value : null ) :
> this.attr( "value", val ).trigger("change");
> }
>
> });
>
> I just append the .trigger("change") to the original code. This would fire
> off the onchange event after the value has been updated.
>
> -Dan



[jQuery] Re: val() and change()

2007-06-12 Thread Dan G. Switzer, II

Josh,

>I was just wondering, should a val() call invoke a change event?  If
>not, is there a good way to emulate this behavior.

You could overwrite the default behavior:

$.fn.extend({
val: function( val ) {
return val == undefined ?
( this.length ? this[0].value : null ) :
this.attr( "value", val ).trigger("change");
}
});

I just append the .trigger("change") to the original code. This would fire
off the onchange event after the value has been updated.

-Dan