Skip to content

Instantly share code, notes, and snippets.

@nickel
Created October 12, 2009 02:23
Show Gist options
  • Save nickel/208071 to your computer and use it in GitHub Desktop.
Save nickel/208071 to your computer and use it in GitHub Desktop.
function newElement(object_name, method, resource, controller, div, prompt_text){
var name = prompt(prompt_text);
if(name) {
field = object_name + "[name]";
$.post("/" + controller, {'element[name]' : name}, function(response) {
var html = "<select name='" + object_name + "[" + method + "]' id='" + object_name + "_" + method + "'><option value=''/>";
for(var i=0;i<response.length;i++){
html += "<option value='" + response[i][resource].id + "'>" + response[i][resource].name + "</option>"
if(response[i][resource].name == name) {
var id = response[i][resource].id
}
}
$("#" + div).html(html);
$("#" + object_name + "_" + method).children("[value=" + id + "]").attr("selected", "selected")
}, "json");
} else {
window.alert("Debe introducir un nombre");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment