Husna
Husna

Reputation: 1386

how to pass value dynamically in jquery

I want to get current class value <input type="text" id="contact_name1" name="name1"/> In js i want to call like this $('#contact_name1').on('input', function() { }.

my question is how to write after underscore current class. $(#contact_"")? name1 changes for all classes.

Upvotes: 1

Views: 455

Answers (3)

Paul Tann&#233;
Paul Tann&#233;

Reputation: 77

i made this exemple few days ago

<script>

$("input").click(function(){ 
    var res="#"+$(this).attr('id') 
    $(res).show() 
});
</script>

just create a variable who contains the entire text and after this you can lauch your function :)

Upvotes: 1

Roko C. Buljan
Roko C. Buljan

Reputation: 206048

You could use the starts with ^= operator inside the attribute selector:

$("[id^='contact_']").on("input", fn);

$("[id^='contact_']").on("input", function() {

  var suffix = this.id.split("_")[1];
  console.log( "ID: %s SUFFIX: %s", this.id, suffix );

});
<input id="contact_name" name="name" type="text">
<input id="contact_surname" name="surname" type="text">
<input id="contact_email" name="email" type="text">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 2

patilprashant6792
patilprashant6792

Reputation: 353

I may suggest using starts with selector. and you can get your class name like this:

 $('input[id^="contact_"]').on('input', function(e) { 

var className = $(e.currentTarget).prop("class")

}

Hope it helps

Upvotes: 1

Related Questions