开发者

JQuery get Value of an Element: onfocus="inputInlineHint(self);"

开发者 https://www.devze.com 2022-12-21 02:55 出处:网络
i have an input field which calls the function \"inputInlineHint(self)\" on blur and on focus. It also passes self...

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!


  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


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
   );
);


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.
    });    
});
0

精彩评论

暂无评论...
验证码 换一张
取 消