1

I have the class of .addy applied to multiple inputs. I don't want to write a bunch of functions with the same code, so I would like to have if statements inside that tell function which parts to execute. This depends on where the action originates from: input#address1 or input#address2.

I need help with my if statement, please. How can I tell the function whether the #address1 or #address2 was where request originated?

$("input.addy").on('change', function(e) {
    e.preventDefault();
    var uid = $('form#data input[name=user_id]').val();
    var bid = $('form#data input[name=business_id]').val();
    if () { // If the change was made to input#address1, do the below
        var address1 = this.value;
        var data = {user_id: uid, business_id: bid, address1: address1};
        $.ajax({
            url: '/business/assignment/saveaddressone',
            type: 'POST',
            dataType: 'JSON',
            data: data,
            success: update
        })
    }
    if () { // If the change was made to input#address2, do the below

        // Here is address2 code
    }
});
7
  • stackoverflow.com/questions/14897768/… and use $(this) instead. Commented Feb 14, 2015 at 2:14
  • Not too sure on what exactly you're trying to do Commented Feb 14, 2015 at 2:15
  • @HugoSousa, that doesn't help thanks though Commented Feb 14, 2015 at 2:15
  • 1
    Just compare the ids with $(this).attr ("id") == "address1" Commented Feb 14, 2015 at 2:16
  • thanks @YerkoPalma - that was it! too easy Commented Feb 14, 2015 at 2:19

3 Answers 3

1

You want to check the id of the changed input, which you can access with $(this).attr(id).

$("input.addy").on('change', function(e) {
    e.preventDefault();
    var uid = $('form#data input[name=user_id]').val();
    var bid = $('form#data input[name=business_id]').val();
    if ($(this).attr('id') == "address1") { //if the change was made to input#address1, do the below
        var address1 = this.value;
        var data = {user_id: uid, business_id: bid, address1: address1};
        $.ajax({
            url:'/business/assignment/saveaddressone',
            type: 'POST',
            dataType:'JSON',
            data:data,
            success: update
        })
    } 
    if ($(this).attr('id') == "address2") { //if the change was made to input#address2, do the below
        //here is address2 code
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1
 e.target.id === 'address1'

or

 $(e.target).is('#address1')

But, rather than use an if, this is exactly where you would use a function that returns another function.

function doStuff(wrappedFunctionality) { return function doStuff(e) {
   var $el = $(e.target);
   //do common stuff here
   wrappedFunctionality($el);
   //do more common stuff
} }

$('#address1').on('change', doStuff(function($el) {
   //do address1 specific things
}))

$('#address2').on('change', doStuff(function($el) {
   //do address2 specific things
}))

Obviously give functions good names that reflect what they're actually doing, not doStuff and wrappedFunctionality.

Learn how to write composable functions, it is the absolute heart of how to write idiomatic js.

Comments

0

Check the ID of the input.addy that has been changed?

if($(this).attr('id') == "address1") {
    // it is input#address1
    ...
} else if ($(this).attr('id') == "address2") {
    // it is input#address2
    ...
}

3 Comments

Why wrap this in jquery? you can do this.id or e.target.id
@GeorgeMauer I am a jQuery fan :)
Even then, prop is more appropriate than attr here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.