10

I am creating a survey form that needs to have each question and set of answers highlighted (by changing the background color) when the user focuses on them. .focus() and .blur() both work in Firefox and IE, but not entirely in Safari and Chrome. I also tried .focusin() and .focusout() with the same results. EDIT: Clicking does not fire the focus event, but tabbing through the input fields does. I say not entirely because it works for text inputs, select inputs and textarea inputs; but not radio and checkbox inputs.

$(document).ready(function()
 {
    $("form li").focusin(function()
  {
   $(this).addClass("over");
  }).focusout(function()
  {
     $(this).removeClass("over");
  });
 });

This is being applied to blocks of html similar to this:

<li>
    <label for="webmail" class="desc">Email</label>
    <input type="text" name="webmail" id="webmail" />
</li>
<li>
    <label for="business" class="desc">Purpose of your Charter Flight:</label>
    <div>
        <span>
            <input type="radio" name="purpose" id="business" class="radio" />
            <label class="choice" for="business">Business</label>
        </span>
        <span>
            <input type="radio" name="purpose" id="pleasure" class="radio" />
            <label class="choice" for="pleasure">Pleasure</label>
        </span>
    </div>
</li>

I tried messing around with toggles, but I am looking for a more elegant solution that doesn't involve using convoluted logic to make it work. Any ideas?

Eric
  • 135
  • 1
  • 1
  • 6
  • If `blur()` isn't working, try creating a dummy input element, sticking it somewhere off the page, and calling `focus()` on it. – Warty Jul 27 '10 at 22:01

3 Answers3

3

I also came across this problem a long time ago, and oddly enough it was a radio button giving me the grief.

Try using jQuery.change() instead, here's a working example from that problem I had

$("input#zendesk-dropbox-askedbefore, input#zendesk-dropbox-newhere").change(function() {
    $("label#zendesk-dropbox-label-askedbefore, label#zendesk-dropbox-label-newhere").toggleClass('zendesk-dropbox-label-highlight');
});

In case it's unclear from my crazy use of ID names, input#zendesk-dropbox-askedbefore & input#zendesk-dropbox-newhere are both radio buttons.

jakeisonline
  • 1,206
  • 1
  • 11
  • 24
  • Thanks for the help, but I ended up figuring it out on my own. This is definitely good to know though for selects, radios, and checkboxes. It has limited use for text fields and textareas because the event triggers AFTER you exit focus on the field, which isn't exactly what I was looking for, but could have certainly been used to brute force the radio buttons and checkboxes to work in Chrome/Safari – Eric Jul 28 '10 at 21:38
  • I encountered the same problem, text inputs and radio inputs had the event "focus" but the radio did not fired this event in Webkit, so these were put on the "change" event. Now I have two events, "focus" for text inputs and "change" for the radio, both call the same callback. – Alejandro García Iglesias Nov 22 '11 at 16:13
1

I thought about it on the drive home last night and figured it out on my own, it may not be the most elegant solution, but it certainly works.

$(document).ready(function()
    {
       $("textarea, input, select, .radio, .checkbox").click(function()
        {
            $("form li.over").toggleClass("over");
            $(this).parents("li").toggleClass("over");
        });
    });

Thanks anyway!

Eric
  • 135
  • 1
  • 1
  • 6
  • 1
    You should also include the relevant click event to the label of the input values, and what happens when users use tab to navigate your form? – jakeisonline Jul 28 '10 at 21:58
  • Would you clarify what you mean by relevant click event? Do you mean adding the onclick attribute to the label? Also, why? I'm pretty new to the whole JavaScript thing all around, so any help is useful! I would love it if .blur() and .focus() worked correctly on mouse click in webkit, but the client wanted it to work when he clicked on it. Do you know of an elegant way to add the .focus() and .blur() functionality without repeating the code a second time with those event handlers? – Eric Aug 02 '10 at 19:37
  • As @jakeisonline pointed out, you'll be in trouble when user uses tab or clicks the label, two standard ways of navigate through a form. I suggest leaving the focus event for text inputs and adding a change event to radios and checkboxes as I suggest as a comment in the other answer. – Alejandro García Iglesias Nov 22 '11 at 16:42
0
// In js area write following method
$('#chkRikin').focus(function() {
alert('Say Hi Rikin');
});


// in html area write following code anenter code hered test it your self
<input type="checkbox" onclick="this.focus()" onblur="yourMethod()" />

it works in google crom
Rikin Adhyapak
  • 483
  • 5
  • 8