Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts

    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,
    Last edited by WolfShade; 02-15-2013 at 05:08 PM.
    ^_^

    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".

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    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

  • #3
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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".

  • #4
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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".


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •