Mouseover/MouseOut jquery - javascript

I realize how simple this question should be to answer but I am in a medication fog and the answer is escaping me.
I would like to make this into a simple function to display specific text if the value of the text box is empty upon mouseout and to empty out the text value upon mouseover.
What I have right now that works but is very ugly:
$(".disappearOnClick").live('mouseover',function() {
if($(this).val() === 'BFA Offset') {
$(this).val('')
}
});
$(".disappearOnClick").live('mouseout',function() {
if($(this).val() === '') {
$(this).val('BFA Offset')
}
});

You can bind to multiple events using the live() method - so you could use something like this ->
$('.disappearOnClick').live('mouseover mouseout', function(event) {
if (event.type == 'mouseover') {
if($(this).val() === 'BFA Offset') {
$(this).val('');
}
} else {
if($(this).val() === '') {
$(this).val('BFA Offset');
}
}
});

$(".disappearOnClick").mouseover(function(){...});
and
$(".disappearOnClick").mouseout(function(){...});
Would work just as well.

You should use hover instead:
$(".disappearOnClick").hover(
function(){
//mouseover
},
function(){
//mouseout
}
);

You could try something like this (you could of course change the focus/blur events to mouse-events):
http://jsfiddle.net/BD7JA/2/
// <input value="BFA Offset" data-placeholder="BFA Offset" class="is-placeholder" />
$('[data-placeholder]').on({
focus: function (evt) {
if ($(this).hasClass('is-placeholder')) {
$(this).val('');
$(this).removeClass('is-placeholder');
}
},
blur: function (evt) {
if ($(this).val() === '') {
$(this).val($(this).data('placeholder'));
$(this).addClass('is-placeholder');
}
}
});

Try this:
$(".disappearOnClick").mouseenter( function (this) {
if ($('#'+this.id).val() == 'BFA Offset')
$('#'+this.id).val('')
}).mouseleave( function (this) {
if ($('#'+this.id).val() == '')
$('#'+this.id).val('BFA Offset')
});

Related

Combine 2 events when document loads

How can I bind 2 different events when the document loads?
I have a text field and a button. The function should be executed either when the button is clicked:
$(document).ready(function() {
$("button").click(function() {
myFunction();
});
});
or when Enter is pressed:
$("#id_of_textbox").keyup(function(event){
if(event.keyCode == 13){
myFunction();
}
});
But how to combine both events?
Did you want this:
$(document).ready(function() {
$("button").click(function() {
myFunction();
});
$("#id_of_textbox").keyup(function(event){
if(event.keyCode == 13){
myFunction();
}
});
});
You can bind your function to as many events as needed, here's one way...
$(document).ready(function() {
$("button").click(myFunction);
$("#id_of_textbox").keyup(myFunction);
});
function myFunction(event) {
if ((event.type === 'keyup') && (event.keyCode !== 13)) {
return;
}
// process event here
}

jquery problems on click()

<script type="text/javascript">
$(document).ready(function(){
$("input[name='trim']").click(function (event) {
if ($(this).val() == "deluxe") {
$("input[name='option']").attr("checked", true);
} else if ($(this).val() == "plain") {
$("input[name='option']").attr("checked", false);
}
});
$("input[name='option']").click(function(event){
$("#custom").attr("checked","checked");
});
});
</script>
This is radio and click button problem. First I click deluxe button, check button works fine. Then I click plain button it works fine too. Then I re-click deluxe button, check button won't working. Then I try to test out custom button. It not working either after I click plain button. Does anyone know whats going on? By the way plain works fine from beginning.
You should use jQuery prop() instead of attr(). attr() is buggy.
Use the following code which is a great jQuery extension. It will accept 1,0,'1','0',true,false, and unidentified. To see in action see my fiddle I posted your usage as well.
(function( $ ) {
$.fn.checked = function(value) {
if(value === true || value === false) {
// Set the value of the checkbox
$(this).each(function(){ this.checked = value; });
} else if(value === undefined || value === 'toggle') {
// Toggle the checkbox
$(this).each(function(){ this.checked = !this.checked; });
} else if(value === 1 || value === '1') {
$(this).each(function(){ this.checked = true; });
} else if(value === 0 || value === '0') {
$(this).each(function(){ this.checked = false; });
}
return this;
};
})( jQuery );
Your usage would be like so:
$("input[name='trim']").click(function (event) {
if ($(this).val() == "deluxe") {
$("input[name='option']").checked(1);
} else if ($(this).val() == "plain") {
$("input[name='option']").checked(0);
}
});
$("input[name='option']").click(function(event){
$("#custom").checked(1);
});

How to add a class to a div on load?

Here's what I'm using
$(document).ready(function() {
$(".accept").change(function () {
if ($(this).val() == "0") {
$(".generateBtn").addClass("disable");
} else {
$(".generateBtn").remove("disable");
}
});
});
It works after you change the value, but how do I add the style to the div on load to disable?
Do I simpily juse call
$(".generateBtn").addClass("disable");
Or is there a more elegant technique
How about just triggering a change ?
$(document).ready(function() {
$(".accept").on('change', function () {
if ($(this).val() == "0") {
$(".generateBtn").addClass("disable");
} else {
$(".generateBtn").remove("disable");
}
}).trigger('change');
});
I'm guessing you meant to write removeClass and not remove, if so :
$(document).ready(function() {
$(".accept").on('change', function () {
$(".generateBtn").toggleClass('disable', this.value=='0');
}).trigger('change');
});

Easy modification to jQuery code to make use of more than one textbox

How to change the following code to make use of all my 4 textboxes with id extra1, extra2,persons and tables?
$(document).ready(function () {
$('#extra1').blur(function () {
if ($.trim(this.value) == "") {
$('#btnUpdate').attr("disabled", true);
}
else {
$('#btnUpdate').removeAttr("disabled");
}
});
});
$('#extra1').blur(function () {
to
$('#extra1, #extra2, #persons, #tables').blur(function () {
The code modification would be:
$(document).ready(function () {
$('#extra1, #extra2, #persons, #tables').blur(function () {
if ($.trim(this.value) == "") {
$('#btnUpdate').attr("disabled", true);
}
else {
$('#btnUpdate').removeAttr("disabled");
}
});
});
But, why not give all of them a class, and use that class as the selector? That way, you can easily add elements later without changing your Javascript.
Use jQuery delegate which will bind only one event handler but will act only on the element which triggers the event. Try this
Wokring demo
$(document).ready(function () {
$('formSelector').delegate('input[type=text]', 'blur', function () {
if ($.trim(this.value) == "") {
$('#btnUpdate').attr("disabled", true);
}
else {
$('#btnUpdate').removeAttr("disabled");
}
});
});

Why can't javascript/jquery-feature be applied to multiple nodes?

I have a small javascript that changes the title of a input-field to it's value and some other stuff:
function autoFill(id){
if(jQuery(id).val()==""){
jQuery(id).val(jQuery(id).attr("title"))
.addClass("help");
};
jQuery(id).focus(function(){
if(jQuery(this).val()==jQuery(this).attr("title")){
jQuery(this).val("").removeClass("help");
}
})
.blur(function(){
if(jQuery(this).val()==""){
jQuery(this).val(jQuery(this).attr("title"))
.addClass("help");
}
});
jQuery(".trip").submit(function(){
if(jQuery(id).val() == jQuery(id).attr("title")){
jQuery(id).val('');
}
});
}
When I try to use this script on a class that is on several nodes on one page it only works on the first. For example:
autoFill(".field");
Now I have to make it like this instead:
autoFill("#driver_from");
autoFill("#driver_to");
autoFill("#driver_when");
autoFill("#passenger_from");
autoFill("#passenger_to");
autoFill("#passenger_when");
how do I make it so that it works on every field instead?
Something like this would work:
function autoFill(selector){
jQuery(selector).each(function() {
if(jQuery(this).val() == "") {
jQuery(this).val(jQuery(this).attr("title"))
.addClass("help");
};
jQuery(".trip").submit(function(){
if(jQuery(this).val() == jQuery(this).attr("title")) {
jQuery(this).val('');
}
});
}).focus(function(){
if(jQuery(this).val() == jQuery(this).attr("title")) {
jQuery(this).val("").removeClass("help");
}
}).blur(function(){
if(jQuery(this).val() == "") {
jQuery(this).val(jQuery(this).attr("title"))
.addClass("help");
}
});
}
The important part is the .val() check at the beginning, it's getting the .val() of the first match, you need to handle each separately.
Or, rewrite it as a plugin like this:
(function($) {
$.fn.autoFill = function() {
return this.each(function() {
$(".trip").submit(function(){
if($(this).val() == $(this).attr("title")) {
$(this).val('');
}
});
}).focus(function(){
if($(this).val() == $(this).attr("title")) {
$(this).val("").removeClass("help");
}
}).blur(function(){
if($(this).val() == "") {
$(this).val($(this).attr("title")).addClass("help");
}
}).blur();
};
})(jQuery);
Then you can call it like this:
$(".field").autoFill();

Categories