Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

Could you point how to obtain the control name that has the focus from inside jQuery?

Hi Experts

Could you point how to obtain the control's name that has the focus from inside jQuery?

Thanks in advance.
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

this is what I found:
// Get the focused element:
var $focused = $(':focus');

// No jQuery:
var focused = document.activeElement;

// Does the element have focus:
var hasFocus = $('foo').is(':focus');

// No jQuery:
elem === elem.ownerDocument.activeElement;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eduardo Fuerte

ASKER

Hi

I get similar results you had  before

  
// 1st image part - with addition of .name
 var focused1 = document.activeElement.name;


// 2nd image part
 var focused2 = $(':focus');

    alert(focused1);

    alert(focused2);
// But this finally works (not very elegant)
    var procname;

    if ($('#tab18').is(':focus'))
    {
        procname = '#proc18';    
    }
    else if ($('#tab19').is(':focus'))
    {
        procname = '#proc19';    
    }
    else if ($('#tab20').is(':focus'))
    {
        procname = '#proc20';    
    }
    else if ($('#tab22').is(':focus'))
    {
        procname = '#proc22';    
    }
    else if ($('#tab60').is(':focus'))
    {
       procname = '#proc60';    
    }


    alert(procname);

Open in new window


User generated image
Hi Eduardo: you missed my post :)
var focused2 = $(':focus');

Open in new window

return a jQuery object; to get the name you have to use attr() as I have shown above.
Thank you Mario!
ops... Marco