Skip to content

Instantly share code, notes, and snippets.

@tperrelli
Created April 12, 2018 14:16
Show Gist options
  • Select an option

  • Save tperrelli/fd17c2fe95c8b17b460b15e80687f9f6 to your computer and use it in GitHub Desktop.

Select an option

Save tperrelli/fd17c2fe95c8b17b460b15e80687f9f6 to your computer and use it in GitHub Desktop.

Revisions

  1. tperrelli created this gist Apr 12, 2018.
    25 changes: 25 additions & 0 deletions select-all.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    import { Directive, ElementRef, HostListener } from '@angular/core';

    @Directive({
    selector: 'ion-searchbar[select-all],ion-input[select-all]'
    })
    export class SelectAll {

    constructor(private el: ElementRef) {
    }

    @HostListener('ionFocus')
    selectAll() {
    // access to the native input element
    let nativeEl: HTMLInputElement = this.el.nativeElement.querySelector('input');

    if (nativeEl) {
    if (nativeEl.setSelectionRange) {
    // select the text from start to end
    return nativeEl.setSelectionRange(0, nativeEl.value.length);
    }

    nativeEl.select();
    }
    }
    }