-1

How can I add value to the active input field among all the input fields?

I used autofocus attribute to get "the_field" , it didn't return anything.

AJ-B29
  • 215
  • 1
  • 11
  • `Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers.` – A. Wolff Jul 17 '15 at 17:18
  • I am not quite sure how it supposed to behave? – MaxZoom Jul 17 '15 at 17:25
  • Have no idea what that sample case is supposed to do. Broken code that doesn't work is not a substitute for proper problem and expected behavior descriptions – charlietfl Jul 17 '15 at 17:38

1 Answers1

3

Vanilla JS

First give all the inputs you might want to add stuff to a certain class, lets call it input-field. If you are using vanilla JS you can do the JS like this:

//Get the focused element.
var focused = document.activeElement;

//Check that it is one of the elements you want to add stuff to.
if(hasClass(focused, 'input-field')) {
    //Add whatever you want to add, lets say a number "3".
    focused.value = focused.value + '3';
}

Where hasClass is a function that checks if an element has a particular class (stolen from here):

hasClass(el, cls) {
    if (!el.className) {
        return false;
    } else {
        var newElementClass = ' ' + el.className + ' ';
        var newClassName = ' ' + cls + ' ';
        return newElementClass.indexOf(newClassName) !== -1;
    }
}

Alternatively (as pointed out by Edwin Reynoso), if you are fine with your code not being supported in IE below version 10, you can use classList.contains():

if(focused.classList.contains('input-field')) {
    ...

If you dont want to add the extra class and just check if it is a input with type text you can check for that like this instead:

if(focused.tagName == 'input' && focued.getAttribute('type') == 'text') {
    ...

jQuery

Or if you prefer working with JQuery you can do it without an extra function:

focused = jQuery(':focus');

if(focused.hasClass('input-field')) {
    focused.val(focused.val() + '3');
}

Again, if you want to skip the class and check for an input type text just use this:

if(focused.is('input[type=text]')) {
    ...

Also see this question: "How to get the focused element with jQuery?"

Community
  • 1
  • 1
Anders
  • 8,307
  • 9
  • 56
  • 88
  • Please note that this does not respect where in the text field the cursor is, it will simply append to the end. If that is a problem, let me know and I will try to update my answer. – Anders Jul 17 '15 at 17:43
  • 1
    Ik this is a little old, but do you know that there's `Element.classList.contains()`?? You don't need to implement your own `hasClass()` https://developer.mozilla.org/en-US/docs/Web/API/Element/classList – Edwin Reynoso Aug 05 '15 at 08:23
  • Things never get to old for improvement, so thanks - will update my answer. I did not know about it. It is of course much neater, but the browser support is not so good. You need IE10. – Anders Aug 05 '15 at 08:26