7Keypad
7Keypad

Reputation: 21

How to get previous focussed element?

How to get previous focussed element?

Explanation:

When user clicks phonepad keys, it should not get active or focussed. so that: focus or activeElement returns previous focus element (textarea or input-text) .

<script>
    $(function(){
        $(".phonepad").prop("disabled", true);
        $('.phonepad').mousedown(function() { preventDefault();});
        $('.phonepad input').mousedown(function() { preventDefault();});
        $('.phonepad input').click(function(){
            $(document.activeElement).sendkeys(
                       this.name || this.value);
                    );
        });
    });
</script>  
<div class="phonepad" >
<input type="button" value="7" ><br>
<input type="button" value="4" ><br>
</div><br/>
<input type="text" class="output" /><br/>
<textarea class="output"></textarea><br/>

Upvotes: 2

Views: 3558

Answers (2)

user3205390
user3205390

Reputation: 11

You can use a hidden element to keep track of the element with the current focus. That way it will hold the previous element id when another element gets focus. When a focus enabled element is clicked, its id is stored in the hidden element. When a focus disabled element is clicked, the focus gets put back on the previously focused element and the 'active_focus_enabled_id' hidden element does not get updated. You'll need to use classes to identify which elements are able/unable to receive focus, and make sure all focus enabled elements have an id.

<input type='hidden'id='active_focus_enabled_id' value=''id_with_initial_focus'>

<input type='button' class='focus_enabled' id='id_with_initial_focus' value='Whatever #1'>
<input type='button' class='focus_enabled' id='some_other_id' value='Whatever #2'>
...

<input type='button' class='focus_disabled' value='Phone Input #1'>
<input type='button' class='focus_disabled' value='Phone Input #2'>
...

<script>
$('.focus_disabled').click(function(event) {
  var id_previous = $('active_focus_enabled_id' ).val();
  $(''#' + id_previous).focus();
});

$('.focus_enabled').click(function(event) {
  var id_new = $(event.target).attr('id');
  $('active_focus_enabled_id').val(id_new); 
});
</script>

Upvotes: 1

matthewk
matthewk

Reputation: 1841

Attach a mousedown event handler to those buttons, that calls preventDefault().

Upvotes: 0

Related Questions