2

I have the following code that I would like to simplify. With javascript and jQuery is there an easy way that I could combine these two functions? Most of the code is the same but I am not sure how I could create a single function that works differently depending on what is clicked.

$(document).ready(function () {
    $('#ListBooks').click(ListBooks);
    $('#Create').click(Create);
});

function Create() {
    var dataSourceID = $('#DataSourceID').val();
    var subjectID = $('#SubjectID').val();
    var contentID = $('#ContentID').val();
    if (dataSourceID && dataSourceID != '00' && 
        subjectID && subjectID != "00" &&
        contentID && contentID != "00")
    {
        var e = encodeURIComponent,
            arr = ["dataSourceID=" + e(dataSourceID),
                   "subjectID=" + e(subjectID),
                   "contentID=" + e(contentID)];
        window.location.href = '/Administration/Books/Create?' + arr.join("&");
    } else {
        alert('Datasource, Subject and Content must be selected.');
    }
    return false;
}

function ListBooks() {
    var dataSourceID = $('#DataSourceID').val();
    var subjectID = $('#SubjectID').val();
    var contentID = $('#ContentID').val();
    if (dataSourceID && dataSourceID != '00' &&
        subjectID && subjectID != "00" &&
        contentID && contentID != "00") {
        var e = encodeURIComponent,
            arr = ["dataSourceID=" + e(dataSourceID),
                   "subjectID=" + e(subjectID),
                   "contentID=" + e(contentID)];
        window.location.href = '/Administration/Books/ListBooks?' + arr.join("&");
    } else {
        alert('Datasource, Subject and Content must be selected.');
    }
    return false;
}

4 Answers 4

2

I have tried merging both your functions into a single event trigger.

See if this helps, If I missed out on any other (uncommon to both codes) bit, I can help you edit it.

$(document).ready(function () {
    $('#ListBooks').unbind('click').bind('click', function() {
        iClicked("list");
    });
    $('#Create').unbind('click').bind('click', function() {
        iClicked("create");
    });
});

var url_map = {
    'create':'/Administration/Books/Create',
    'list':'/Administration/Books/ListBooks'
};

function iClicked(trig) {
    if (!url_map[trig]) {
        alert("Unsupported event");
        return false;
    }
    var dataSourceID = $('#DataSourceID').val();
    var subjectID = $('#SubjectID').val();
    var contentID = $('#ContentID').val();
    if (dataSourceID && dataSourceID != '00' &&
        subjectID && subjectID != "00" &&
        contentID && contentID != "00")
    {
        var e = encodeURIComponent,
            arr = ["dataSourceID=" + e(dataSourceID),
                   "subjectID=" + e(subjectID),
                   "contentID=" + e(contentID)];
        window.location.href = url_map[trig] + '?' + arr.join("&");
    } else {
        alert('Datasource, Subject and Content must be selected.');
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Something like that ?

$(document).ready(function () {
    $('#ListBooks').click(function() { yourNewFunction('ListBooks'); });
    $('#Create').click(function() { yourNewFunction('Create'); });
});

yourNewFunction(type) {
  // code of both functions
  if ('ListBooks' == type) {
   // code for ListBook
  } else {
   // code for Create
  }
};

3 Comments

Yeah I think this would be good. I was not sure how to call a function with parameters as you have done. Is there a common standard way for naming functions? I notice you used a lowercase for the first character.
@Melissa: JavaScript commonly uses camelCase with the first letter lowercase for variables and functions.
Acutaly i don't know, probably there are coding standards. Many JS, PHP,... functions starts with lowercase. So i'm building application on the same way.
1

What you need is to make a function perform differnt action based on the id you passed in $('#ListBooks') and $('#Create'). So instead of hardcoding the id into your function, you use this.id instead to retrive the value. Adjusted this line:

$('#ListBooks').click(combinedFunction);

and this

window.location.href = '/Administration/Books/'+ this.id + '?' + arr.join("&");

The final code:

$(document).ready(function () {
    $('#ListBooks').click(combinedFunction);
    $('#Create').click(combinedFunction);
});

function combinedFunction() {
    var divId = this.id;
    var dataSourceID = $('#DataSourceID').val();
    var subjectID = $('#SubjectID').val();
    var contentID = $('#ContentID').val();
    if (dataSourceID && dataSourceID != '00' && 
        subjectID && subjectID != "00" &&
        contentID && contentID != "00")
    {
        var e = encodeURIComponent,
            arr = ["dataSourceID=" + e(dataSourceID),
                   "subjectID=" + e(subjectID),
                   "contentID=" + e(contentID)];
        //Use this.id to retrive the value
        window.location.href = '/Administration/Books/'+ this.id + '?' + arr.join("&");
    } else {
        alert('Datasource, Subject and Content must be selected.');
    }
    return false;
}

Comments

1
function doBookAction(action) {
    var dataSourceID = $('#DataSourceID').val();
    var subjectID = $('#SubjectID').val();
    var contentID = $('#ContentID').val();
    if (dataSourceID && dataSourceID != '00' &&
        subjectID && subjectID != "00" &&
        contentID && contentID != "00") {
        var e = encodeURIComponent,
            arr = ["dataSourceID=" + e(dataSourceID),
               "subjectID=" + e(subjectID),
               "contentID=" + e(contentID)];
        window.location.href = '/Administration/Books/' + action + '?' + arr.join("&");
    } else {
        alert('Datasource, Subject and Content must be selected.');
    }
    return false;
} 

$('#ListBooks,#Create').click(function() { doBookAction($(this).attr('id')); });

Comments

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.