$( document ).ready(function() {
return $("body").on("change",".creator_name_type", function(event){
if (event.target.value == 'Personal') {
$(this).siblings(".organization_name").hide();
$(this).siblings(".given_name").show();
$(this).siblings(".family_name").show();
} else {
$(this).siblings(".given_name").hide();
$(this).siblings(".family_name").hide();
$(this).siblings(".organization_name").show();
}
});
});
$(document).ready(function(){
return $("body").on("click", ".add_creator", function(event){
event.preventDefault();
var creatorClass = $(this).attr('data-addCreator');
var cloneUbiDiv = $(this).parent('div' + `${creatorClass}`).clone();
$(`${creatorClass}` + ':last').after(cloneUbiDiv);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="creator">
<select class="creator_name_type">
<option value="Personal">Personal</option>
<option value="Organisational">Organisational</option>
</select>
given name: <input type="text" name="gname" class="given_name"><br>
family name: <input type="text" name="fname" class="family_name"><br>
organization name: <input type="text" name="oname" class"organization_name"><br>
<a href="#" class=" add_creator" data-addCreator=".creator">Add another </a>
<div>
</body>
</html>
I am trying to selectively display form input based on selected options. The form has buttons for add more which just clones and append the cloned html to the bottom of the div. When I click add more and now have 2 select tags with one having the selected value as 'Personal' and the other a selected value of "organization". Rather than selectively show the form fields based on the if/else statement, it shows thesame form fields for both while retaining different selected value for each dropdown options ie one dropdown shows personal as selected and the other shows Organization. Everytime the on change event fires for one of them, thesame form fields are displayed for both rather than displaying form fields based on the if/else statement.
$( document ).ready(function() {
return $("body").on("change",".creator_name_type", function(event){
if (event.target.value == 'Personal') {
$(".organization_name").hide();
$(".given_name").show();
$(".family_name").show();
} else {
$(".given_name").hide();
$(".family_name").hide();
$(".organization_name").show();
}
});
});