milacay
milacay

Reputation: 171

JQuery - Two textboxes excute the same codes

This should be a simple question, but I am new to JQuery.

The code below will execute when a textbox with ID="CompanyName" entered. I have another textbox with ID="City". How can I put and OR statement, saying that "Either CompanyName or City boxes enter, process the same code below.

Thanks,

   $('#CompanyName').on('keypress', function(e) {
        if (e.keyCode == 13)
        {
            //alert("Found");
            e.preventDefault();
            // Some code here.....
        }
    });

Upvotes: 0

Views: 48

Answers (3)

DevlshOne
DevlshOne

Reputation: 8457

$("#CompanyName, #City").change(function() {
  //alert ("Found);
  .........
  execute code
  .........
)};

Upvotes: 1

Andriy Ivaneyko
Andriy Ivaneyko

Reputation: 22031

Try to specify multiple selectors, separated by ","

$('#CompanyName, #City').on('keypre..

This should fix your issue

Upvotes: 2

LeoPleurodon
LeoPleurodon

Reputation: 305

You could try giving them a common class, and selecting that instead, eg

$('.class-name').on('keypress', function(e) {

Upvotes: 2

Related Questions