I am creating a jQuery function in which I am using one class for a button that is common in second div's button too. my html markup is
<div class="micmstabs miindex">
<h4>Manage Your Index Page Here</h4>
<textarea class="redactor_content" name="content" rows="4"><?php cms_html('index'); ?></textarea>
<a href="#" data-dismiss="modal" class="cms_s index_save btn btn-primary">Save Changes</a>
</div>
<div class="micmstabs miterms">
<h4>Manage Your Terms & Conditions Page Here</h4>
<textarea class="redactor_content" name="content" rows="4"><?php cms_html('terms'); ?></textarea>
<a href="#" data-dismiss="modal" class="cms_s terms_save btn btn-primary">Save Changes</a>
</div>
In the above when we click on cms_s i try to get value of textarea based on a condition check in jQuery that if this button has this class then get me value of the textarea of div in which this button and the textarea is.
My jQuery function is
$(document).on('click', '.cms_s', function(event) {
if ($(this).hasClass('index_save')) {
var cms_page='index';
}
if ($(this).hasClass('terms_save')) {
var cms_page='terms';
}
if ($(this).hasClass('policy_save')) {
var cms_page='policy';
}
if ($(this).hasClass('mem_save')) {
var cms_page='membership';
}
alert(cms_page);
var cms_html=$(this).parents("div").find('.redactor_content').val();
alert(cms_html);
});
I am able to get the condition check for the class perfectly . But when i try to get the value of textarea based on the button i clicked no matter what button i click it gets me the value of the textarea present in the first div markup.
.parent()would also address the issue.