JQuery get Value of an Element: onfocus="inputInlineHint(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!
- Pass
this
notself
.self
is undefined. - 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 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.
});
});
精彩评论