1
votes

I need to find the element clicked which causes a text input to blur and store it as a variable. how can I determine the id of the element that was clicked to cause the input 'textBox' to lose focus? I don't know if I am approaching this the right way, but this code below is what I have so far. I tried document.activeElement, but it only worked on inputs and not elements such as li's and anchor tags Thanks

javascript:

var elClicked;

$('textBox').blur(function(){
  elClicked = // what is the id of the element that caused blur?
  alert(elClicked);
});

html:

 <input type = "text" id = "textBox">

 <!-- example elements --> 

  <input type = button id = "element1" />
  <a href = "#" id = "element2">text</a>
  <ul><li id = "element3">list</li></ul>
2

2 Answers

2
votes

By accessing the id property of the target.

$('textBox').blur(function(){
  var target = event.explicitOriginalTarget || document.activeElement;
  elClicked = target ;
  alert(elClicked);
});

alternatively you could try an approach along these lines

var elClicked;

$(document).mousedown(function(e) {
    elClicked = $(e.target);
});

$('textBox').blur(function(){
  alert(elClicked);
});
0
votes

You can store the id of anchor tag in a hidden field ,When it is clicked.And takeing that value you can find which element is clicked