jquery - JavaScript hide part of input text -


how can hide not delete part of input text,

in case have input text holds ip + name, hide ip keep name, still ip shall still shown using .val();

<input type="text" id="my_url" value="192.168.3.15/name"/> <!-- hide ip --> 
$('#my_url').val(); 

you can in 4 step method.

  1. execute following in blur event.
  2. have data attribute input original value.
  3. change value have desired display value.
  4. on focus, change original value.

snippet better understanding:

$(function () {    $("#my_url").blur(function () {      $(this).attr("data-original", this.value);      console.log(this.value);      this.value = this.value.substr(this.value.indexof("/"));      console.log(this.value.substr(this.value.indexof("/")));    }).focus(function () {      if ($(this).attr("data-original") != undefined)      this.value = $(this).attr("data-original");    });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <input type="text" id="my_url" value="192.168.3.15/name" />

update: denys séguret mentioned in comments, above have issues when value calculated. in case, can combine use of hidden input well:

$(function () {    $("#my_url").on("input keyup", function () {      $("#orig_url").val(this.value);    }).blur(function () {      $(this).attr("data-original", this.value);      $("#orig_url").val(this.value);      console.log(this.value);      this.value = this.value.substr(this.value.indexof("/"));      console.log(this.value.substr(this.value.indexof("/")));    }).focus(function () {      if ($(this).attr("data-original") != undefined)      this.value = $(this).attr("data-original");    });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <input type="text" id="my_url" value="192.168.3.15/name" />  <input type="hidden" id="orig_url" />

and can use #orig_url's value in calculation.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -