Update: In the comments someone noted that this function should really only occur once, the script and demo have been updated with the handy .unclick() to achieve this.
Update2: jQuery creator John Resig has detailed how to reduce and simplify the code for what I intended to do, be sure to check out his comment
Amongst the tidal wave of javascript frameworks, libraries, widgets and kitchen sinks that have come about lately - one in particular has stood out for me: jQuery.
I’ve been looking for a way to make little custom bits of javascript for various things, nothing overly complicated - a show/hide here, a fade/reveal there. If you’re looking for out-of-the-box complicated widgets, you might start elsewhere - or better yet, hire a javascript guru (like we usually do at Twinsparc). For my smaller javascript needs, and my way of thinking, jQuery just makes perfect sense.
In any case, I thought it might be helpful to see how I used jQuery to construct a small function I needed. What follows is a detailed description, from the perspective of a near javascript illiterate.
### What I Was Looking To Do
In order to tighten up a layout, I wanted to take a very simple search box and label it on the inside of the box with a little helper note - then when the user clicks to enter a search term, the note should go away. If javascript is turned off, I want a real HTML **label** to show up outside the box (not as pretty, but accessible). **[Here's the demo page](http://web-graphics.com/pages/jquerryexample/)**.
### The jQuery code
First thing, lets call in the jQuery script on the page in the head:
<script type="text/javascript" src="jquery.js"></script>
Now lets put in the HTML as we’d want it to show without javascript:
<form action="#">
<label for="input\_search">Search</label>
<input type="text" id="input\_search" class="txtinput" />
<input type="image" id="go\_search" src="/img/btn_search.gif"
value="search" width="18" height="19" />
</form>
Now lets add the jQuery call that does what we’re after:
<script type="text/javascript">
$(document).ready(function(){
$(document.getElementById("input\_search")).set("value","Search")
$(document.getElementById("input\_search")).click(function(){
$(document.getElementById("input\_search")).set("value","").unclick();
});
$("#fancysearch label").hide();
});
</script>
### So What Does This Do?
The first and last lines within the script tag get the attention of jQuery - “Hey jQuery, do this stuff in here.”
In the second line, you start off with a $, then (document.getElementById("input\_search")) means: “grab the element with an ID of **input\_search**” or “find the search box”.
That’s followed by a dot and the jQuery action “set” with it’s specifics in parenthesis ("value","Search"), meaning make the value of this thing “Search”.
So far so good - we’ve asked it to find the text input box, which we gave an ID of “input\_search”, and set it’s value to “Search”. This is what puts the helper text inside the search box.
Next up, we’ve got the same “Find the search box” and instead of “.set” we have “.click” followed by (function(){, which is the start of a function. So in other words, we’d like a function to start up when the search box is clicked on. The stuff that occurs on the next line is the stuff we’d like to actually happen on click.
Now we are onto the 4th line - it’s just like the second one, but instead we setting the value of the search box to "", in other words, making it blank. The .unclick(); part makes sure that this only happens the first time, in case someone decides to click out and then click back in, so it’s impossible to erase anything that was actually typed in.
The 5th line closes our what-to-do-on-click function.
The 6th line does the following: “find the **label** inside something with an ID of **fancysearch** and hide it! We’ve named our form here “fancysearch”, so it hides the label for us. This is preferable to hiding it via CSS, because we only want hide it if the script is actually working.
Ok, that’s all there is to it. Take a look at the documentation and examples over at the jQuery site for more information, this example just scratches the surface of course.
Posted in Javascript |
You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.
14 Responses to “Using jQuery”
July 5th, 2006 at 7:19 pm
By the way - if you liked this, don’t miss 15 Days of jQuerry!
July 5th, 2006 at 8:07 pm
Nice, but it will also blank out user input if the field loses focus and the user clicks in again. You can maybe check to see whether the value is the same as your label text before blanking and work from there.
July 5th, 2006 at 8:23 pm
Ah excellent point, I didn’t try that. Update to the script coming soon…
July 5th, 2006 at 10:04 pm
Ok! updated. Really an easy thing to do, I just used .unclick() to make sure that the function only happens once.
July 5th, 2006 at 10:43 pm
Here’s my take on the jQuery code - glad to see you’re digging it:
$(document).ready(function(){ $("#input\_search").val("Search").oneclick(function(){ $(this).val(""); }); $("#fancysearch label").hide(); });A couple items to note:
* You can use “#input\_search” instead of document.getElementById(”input\_search”)
* You can use the new .val() instead of .set(”value”,”foo”)
* You can use .one* if you only want an event to fire once - in this case, .oneclick()
* Also, don’t forget that you can chain operations in jQuery (I chained the val and oneclick methods together, making the code shorter)
Hope this helps!
July 5th, 2006 at 11:32 pm
Oh awesome! Thanks very much for the tips John. I’m amazed that the code can be reduced down so small and still makes perfect sense.
July 8th, 2006 at 4:04 am
Why not just put an onclick this, onblur that on the field and be done with it? If no js the user can delete it.
July 8th, 2006 at 9:38 am
Well I’d prefer the user does not have to delete the text in any circumstance.
September 27th, 2006 at 8:52 am
My problem with this code is that the “Search” text has to be hard coded into the JavaScript. This means the logic is not separated from the page content, and you would have to update the JavaScript every time you changed the XHTML label from “Search” to something else.
I have written a similar function which goes through the document and stores the “value” attribute of input tags into an array called InitialValues, then uses the stored value to set and clear the search box value.
This means you can change the search label to something like “Type here to search” in the XHTML and the JavaScript functionality still works.
I know this article is more about jQuery but its something you might consider building in :)
October 2nd, 2006 at 1:31 pm
To avoid that the “Search” text is hard coded in the JS, you can use the defaultValue property of the input element. In that way, it’s even pretty easy to put the original value back into the field when the users has not entered anything at all.
July 23rd, 2007 at 12:37 am
sdsds
January 29th, 2008 at 10:35 am
Hi, having a bit of trouble with this! I have originally used John’s update and it works only if I move the hide function to the top of the script. BUT the inserted value in the search field stays put.
Seen here: http://www.mikeebee.com/staging/ces/working.htm
If I use it how John originally said it doesn’t work at all
Seen here: http://www.mikeebee.com/staging/ces/notworking.htm
If I use the original script written by Nate it doesn’t work at all either.
Seen here: http://www.mikeebee.com/staging/ces/reallynotworking.htm
I’ve double checked that my path to my jquery is ok and it is but I must be missing something!
Hope someone can spot what I’m doing wrong
Thanks
February 11th, 2008 at 10:57 am
the $(”#fancysearch label”).hide(); is looking for a element with an ID of fancysearch that has a subelement label, Which doesn’t exist in the example above change the form tag
June 26th, 2009 at 12:28 pm
I need to develop a page look like browser job page of http://jobsearch.monster.co.uk/Browse.aspx
in this page user click on links these links add to job filter and then search job .
Its difficult for me..I didnt know any thing about jquery plz help me out
Leave a Reply