Alex Rivera | Logout

jQuery, same function for multiple ids

Asked 2012-06-22T06:30:11.137
14

i want to clear specified input if the value is not number. function works for one ID, but i want it to work for multiple. of course i can write function multiple times but i dont want to do that.

the following code gets effect only for input with id "d". i dont know how to identify other ids. can anyone help?

<input id="d" />

<input id="d2" />

<input id="d3" />

<script type="text/javascript">

$('#d,d2,d3').keyup(function(){

if($('#d,d2,d3').val() != "") {

    var value = $('#d,d2,d3').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');

    var intRegex = /^\d+$/;

    if(intRegex.test(value)) {}

    else {


    $(this).val('');

    }



}

});

</script>
Edit
Report

1 Answer

3

You can add class attribute

   <input id="d" class="A"/> 
    <input id="d2" class="A"/> 
    <input id="d3" class="A"/> 

use following selector by class name

$('.A').keyup
answered 2012-06-22T06:35:04.290

Your Answer