Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-14-2013, 09:29 PM   PM User | #1
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
Question jQuery + regex mask not working as expected

Hello, everyone.

I've got a keyword search form that is appearing on every page. The text input defaults to a value of "Search...".

I've written code that is supposed to bind to the input a focus and a blur. On focus, if the value == "Search...", make the value blank. On blur, if the value == "", make the value "Search...". If the value is anything else, don't do anything.

The blur part works just fine; but if you type a word into the input, then blur, then put focus back on the input, it still blanks the value. I can type the word "account" into the input, blur (keeping the value "account") and then put focus back and "account" disappears.

Here is my code that is within $(document).ready(). Can anyone see what I'm doing incorrectly?

Code:
$('#searchBox').bind({
  focus: function(){
    if($(this).val().replace(/^\s*|\s*$/gi,'') == "Search..."){$(this).val('');}
    }
  blur: function(){
    if($(this).val().replace(/^\s*|\s*$/gi,'') == ""){$(this).val('Search...');}
    }
});
Thanks,
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

Last edited by WolfShade; 02-15-2013 at 05:08 PM..
WolfShade is offline   Reply With Quote
Old 02-15-2013, 07:26 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
You have a syntax error in your JSON-Objekt (missing comma)
Code:
$('#searchBox').bind({
  focus: function(){
    if($(this).val().replace(/^\s*|\s*$/gi,'') == "Search..."){$(this).val('');}
    },
  blur: function(){
    if($(this).val().replace(/^\s*|\s*$/gi,'') == ""){$(this).val('Search...');}
    }
});
Then it will work: http://jsbin.com/avikon/1/edit
devnull69 is offline   Reply With Quote
Old 02-15-2013, 01:40 PM   PM User | #3
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
Sorry.. since I cannot copy/paste and had to type it, manually, I missed the comma. I just looked at the code in the document and the comma is there.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 02-15-2013, 05:07 PM   PM User | #4
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
Nevermind. Another developer failed to tell me that there was _almost_ similar code in an external .js file that is included with every page.

:/
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Reply

Bookmarks

Tags
blur, focus, jquery, mask, regex

Jump To Top of Thread


Thread Tools
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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:16 PM.


Advertisement
Log in to turn off these ads.