views:

152

answers:

3

Hi Guys,

i have an input field which calls the function "inputInlineHint(self)" on blur and on focus. It also passes self...

<input type="text" id="one" class="textbox" value="Your Name" onfocus="inputInlineHint(self);" onblur="inputInlineHint(self);" />

Now i'd like to retrieve the current value of the field:

function inputInlineHint(e) {

  var theValue = '';

  //Now i need to retrieve the value... but how?
}

I hope you guys can help me with that... should be pretty basic but i'm new to jquery.

Thanks in advance!

+3  A: 
  1. Pass this not self. self is undefined.
  2. Rename the variable from e to something else. e is traditionally used to receive an event object, but you aren't assigning the function as an event handler
  3. whatever_you_renamed_e_to.value
David Dorward
Thank you very much. That is what i was looking for!
n00b
+3  A: 

Seeing as you've tagged your question with jQuery, the jQuery solution would be something like this:

$(document).ready(function() {
    $('#one').blur(function() {
        var val = $(this).val();
        // rest of processing, etc.   
    })
    .focus(function() {
        var val = $(this).val();
        // rest of processing, etc.
    });    
});
Ian Oxley
you should use $(function() {}); instead of $(document.ready(function(){});, it's considered better practise (saves some bytes ;)
Ed Woodcock
Thank you as well!
n00b
+3  A: 

You are much better off using a jQuery binding function as opposed to writing the script name into the control:

$(function() {
   $('#one').focus(function() {
      var value = $(this).val();  /// this is now the value
   }).blur(function (){
      var value = $(this).val();  /// same again
   );
);

Ed Woodcock
I will do that in the future... for now I'll be using this quick and dirty solution!
n00b
Well, you're just setting yourself up for a maintanence headache. Also, what you're doing isn't really classified as jQuery, more just javascript.
Ed Woodcock
Okay you're right... but somehow it doesn't work...I included the latest version of jquery as well as another .js file which contains your code.But when i click the text field nothing happen (of course i alert the var value)any idea why?
n00b
No, not without seeing the full setup of your site. Have you tried debugging through it with firebug to see if the functions are actually getting called? If not, there's likely an issue with your ID on the text box, (the $('#one') bit takes the id as it's argument, see?), or you've included the files in the wrong order (jQuery has to go first)
Ed Woodcock