javascript - Handle many IDs -
javascript - Handle many IDs -
i making character counter input fields in form. script, when focus on input field, when leave it, , when finish pressing key:
$(document).ready(function () { $("#in1").keyup(function () { $("#count1").text(50 - this.value.length); }); $("#in1").focus(function () { $("#countainer1").show(); $("#count1").text(50 - this.value.length); }); $("#in1").blur(function () { $("#countainer1").hide(); }); });
you can see working here: http://jsfiddle.net/uqfd6/12/
the question is:
do have generate script each id?
i mean, code same, applied different ids: #input1
shows #countainer1
, modifies #count1
, #input2
#countainer2
, #count2
, , on.
i thought of using classes instead of ids, when shows container, show every container of class, instead of 1 field i'm writing in.
no, can utilize class instead of #in1
, e.g. add together in class elements give id in1, in2, etc.
$(".in").keyup(function () {
you can utilize attribute e.g. data-id=1
on elements need select associated current #in1
, e.g.
var id = $(this).data('id'); $(".count[id='" + id + "']").text(...);
javascript jquery
Comments
Post a Comment