views:

80

answers:

2

I am using jQuery to show and hide a div by adding/removing a class to it.

$('input').focus(function(){
    $(this).parents('.row').addClass("linksdiv");
}).blur(function(){
    $(this).parents('.row').removeClass("linksdiv");
});

It works quite well when focusing on inputs, but if I click a link in linksdiv it loses focus and the div disappears. Would it be better to use show() and hide() for the linksdiv than to depend on css?

Would that allow the div to be clickable when inputs are focused? Or is there a simple work around to keep linksdiv from losing focus when clicked but still have it disappear on blur?

Thanks again in advance! You folks are fantastic!

I am sorry I couldn't describe very well what i was trying to do this is it http://jsfiddle.net/Zw5c2/5/ Thanks Patrick for the resource!

A: 

I would not have a blur event attached to the inputs. Blur happens for many reasons and which can be undesired.

If your goal is to simply highlight the input row for the currently active field, then I would just add logic within focus that ensures that only one div.row can have a linksdiv class at a time. In your case:

$('input').focus(function() {
  var containing_div = $(this).parents('.row');
  var current_div = $('div.row.linksdiv');

  if (current_div && current_div != containing_div) {
    current_div.removeClass("linksdiv");
  }
  else {
    containing_div.addClass("linksdiv");
  }
});
Jason McCreary
I think I can catch the drift but that didnt seem to have the desired effect. This is what I am doing http://jsfiddle.net/Zw5c2/5/
CarterMan
Maybe you could explain the *desired effect*.
Jason McCreary
The desired effect would be the exact same thing I have in the sample except you can click the link before the div disappears.
CarterMan
A: 

@Nick is right, there won't be a clean solution.

One possibility is to delay the code in your blur so that your link's click handler has a chance to send the focus back to the input.

http://jsfiddle.net/Zw5c2/

var lastInput;
var timeout;
$('input').focus(function() {
    var $th = $(this);
    lastInput = $th;
    clearTimeout(timeout);
    $th.parents('.row').addClass("linksdiv");
}).blur(function() {
    var $th = $(this);
    timeout = setTimeout(function() {
        lastInput = null;
        $th.parents('.row').removeClass("linksdiv");
    }, 150);
});

$('.link').click(function() {
    lastInput.focus();
    // run your code for the link
});

Another solution may be to make it so that the link is actually an input. Just style it to make it look like a link, and prevent any change to the text.

http://jsfiddle.net/Zw5c2/1/

Neither solution is perfect.

A change to implementation may be better.

patrick dw
I should have been more specific this is what I am trying. http://jsfiddle.net/Zw5c2/5/ Rather difficult to explain. Your first solution came very close but I had trouble with the blur still.
CarterMan
@CarterMan - If you simply want to be able to click the link, then you'll still need to delay the blur. I updated your jsFiddle to add a `setTimeout`. Now you can click the link. http://jsfiddle.net/Zw5c2/7 But if it is going to take you to another page, I don't understand why you would need to hide the `.row`. There are different approaches *depending on what the link is actually supposed to do*.
patrick dw
The link loads either a page of FAQs or resources pertaining to the input to be filled in. The hide class row is needed so not all fields in form are highlighted at the same time. It is assumed that the link will be equal to `target=_blank`
CarterMan
@CarterMan - If you're not actually leaving the page, instead of using the `setTimeout()` solution, you could change the `click` event on the link to be a `mousedown` event which should fire before the `blur`.
patrick dw
would setting it before the blur also allow it to work in IE?otherwise mousedown did the trick THANK YOU!
CarterMan
@CarterMan - Did a quick test in IE7 and IE8, and it seems to be consistent. I make no promises with regard to IE, though. ;o)
patrick dw
Ok so I appended it like so http://jsfiddle.net/Zw5c2/11/ but I cant seem to get it to take in IE8 even. Am I missing something. Sorry for the trouble I am new to this.
CarterMan
@CarterMan - I'm not sure what the `.link` is supposed to do when you click it, but I was thinking of something a little more simple, like this: http://jsfiddle.net/Zw5c2/8/ I gave the `.link` a little functionality by having it append the word "hi" when you click it. Works for me in IE8.
patrick dw