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.
- execute following in
blur
event. - have data attribute input original value.
- change value have desired display value.
- 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
Post a Comment