I'm trying to pick up the class of an element with a certain id as follows. I'm going to have multiple fields generating two dropdowns with "Add New" options, but with different classes for the options, "animal_species" in the first case, "animal_condition" in the second. When "Add New" is selected in the dropdown a modal window pops up with a text field in which to enter a new option to the dropdown. Here's the modal window:
<!-- add-new field -->
<div class="modal small hide fade" id="add-new" tabindex="-1" role="dialog" aria-labelledby="add-new-fieldLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">�?�ó</button>
<h3 id="add-new-fieldLabel">Add New Field</h3>
</div>
<div class="modal-body">
<p>Would you like to add a new item?</p>
<input type="text" id="add-new-text" name="add-new-text" placeholder="Type the new option">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" id="add-new-submit" name="add-new-submit"/>Add</button>
</div>
</div><!-- /add-new field -->
The jQuery for the text field is
$('#upload_form option[value="addnew"]').click(function(){
// Show modal window
$('#add-new').modal('show');
$('#add-new-submit').on('click', function(){
// Get new option from text field
var value = $('#add-new-text').val();
console.log(value);
// Get the class
//var Classofentry = $('#upload_form option[class]').attr("class");
var Classofentry = $('#upload_form option[class]').attr("class");
console.log(Classofentry);
$.ajax({
type: "POST",
url: "<?php echo site_url(); ?>main/change_options",
data: {new_option: value, new_option_class: Classofentry},
dataType: "html",
error: errorHandler,
success: success
});
function success(data){
//$('#animal_species').append("<option value='" + data + "'selected=\"selected\">" + data + "</option>"); // This might work ...
$('#'+Classofentry).append("<option value='" + data + "'selected=\"selected\">" + data + "</option>"); // This might work ...
alert(data);
//alert('Success!');
}
function errorHandler(){
alert('Error with AJAX!');
}
$('#add-new').modal('toggle');
});
});
</script>
Which works pretty well except that now that I've added the second "Animal Condition" element, the text entered in the modal gets put into the first ("Animal Species") element because "Classofentry" is always "animal_species" and not the class of the element that spawns the modal. How can I access that class in jQuery? Or maybe I need a different implementation for multiple fields? Been trying numerous things but nothing's falling into place yet. Help!
Here's how I got it to work (thanks for the comments!):
<script type="text/javascript">
$('#upload_form option[value="addnew"]').click(function(){
// Show modal window
$('#add-new').modal('show');
// Get the class
//var Classofentry = $('#upload_form option[class]').attr("class");
var Classofentry = $(this).attr("class");
console.log(Classofentry);
$('#add-new-submit').on('click', function(){
// Get new option from text field
var value = $('#add-new-text').val();
console.log(value);
$.ajax({
type: "POST",
url: "<?php echo site_url(); ?>main/change_options",
data: {new_option: value, new_option_class: Classofentry},
dataType: "html",
error: errorHandler,
success: success
});
function success(data){
//$('#animal_species').append("<option value='" + data + "'selected=\"selected\">" + data + "</option>"); // This might work ...
$('#'+Classofentry).append("<option value='" + data + "'selected=\"selected\">" + data + "</option>"); // This might work ...
alert(data);
//alert('Success!');
}
function errorHandler(){
alert('Error with AJAX!');
}
$('#add-new').modal('toggle');
});
});
</script>
I moved the $(this) element up to access the selected option.
$('#upload_form option[class]').attr("class")
will always get the class of the first matched element, it has no concept of current element. You should select the elements based on the current elementthis
or the selected option:selected