So I have this code here that has a certain amount of dropdown lists preloaded and hidden depending what is calling this partial view.
On the add button click I am attempting to select the first element that is hidden and show it.
For some reason it is not working with the :hidden:first tag I am using to attempt to select it.
Here is my code below
@model Client_Portal.Models.ChartYearViewModel
@using MoreLinq;
@{
/**/
var divtoupdate = ViewBag.DivName;
ViewBag.Title = "Home";
}
<div id="updateTableChartYearsModel">
@*@using (Ajax.BeginForm("StaffCreateSubmit", "Home", new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "updateTableChartYearsModel"
}
, new { id = "updateTableChartYearsForm" }))
{*@
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="form-group row year" id="year1">
@Html.LabelFor(model => model.Year1, htmlAttributes: new { @class = "col-lg-3 col-md-3 col-sm-12 col-xs-12 col-form-label", @id = "" })
<div class="col-lg-8 col-md-8 col-sm-12 col-xs-12">
@Html.DropDownListFor(model => model.Year1, new SelectList(Model.YearList1, "Value", "Text"), new { @class = "form-control" })
</div>
<div class="col-lg-1 col-md-1 col-sm-1 col-xs-1"><span class="glyphicon glyphicon-remove removeYear" style="color:red; font-size:18px; top:8px"></span></div>
</div>
<div class="form-group row year" id="year2">
@Html.LabelFor(model => model.Year2, htmlAttributes: new { @class = "col-lg-3 col-md-3 col-sm-12 col-xs-12 col-form-label", @id = "" })
<div class="col-lg-8 col-md-8 col-sm-12 col-xs-12">
@Html.DropDownListFor(model => model.Year2, new SelectList(Model.YearList2, "Value", "Text"), new { @class = "form-control" })
</div>
<div class="col-lg-1 col-md-1 col-sm-1 col-xs-1"><span class="glyphicon glyphicon-remove removeYear" style="color:red; font-size:18px; top:8px"></span></div>
</div>
<div class="form-group row year" id="year3">
@Html.LabelFor(model => model.Year3, htmlAttributes: new { @class = "col-lg-3 col-md-3 col-sm-12 col-xs-12 col-form-label", @id = "" })
<div class="col-lg-8 col-md-8 col-sm-12 col-xs-12">
@Html.DropDownListFor(model => model.Year3, new SelectList(Model.YearList3, "Value", "Text"), new { @class = "form-control" })
</div>
<div class="col-lg-1 col-md-1 col-sm-1 col-xs-1"><span class="glyphicon glyphicon-remove removeYear" style="color:red; font-size:18px; top:8px"></span></div>
</div>
<div class="form-group row year" id="year4" style="display:none">
@Html.LabelFor(model => model.Year4, htmlAttributes: new { @class = "col-lg-3 col-md-3 col-sm-12 col-xs-12 col-form-label", @id = "" })
<div class="col-lg-8 col-md-8 col-sm-12 col-xs-12">
@Html.DropDownListFor(model => model.Year4, new SelectList(Model.YearList4, "Value", "Text"), new { @class = "form-control" })
</div>
<div class="col-lg-1 col-md-1 col-sm-1 col-xs-1"><span class="glyphicon glyphicon-remove removeYear" style="color:red; font-size:18px; top:8px"></span></div>
</div>
<div class="form-group row year" id="year5" style="display:none">
@Html.LabelFor(model => model.Year5, htmlAttributes: new { @class = "col-lg-3 col-md-3 col-sm-12 col-xs-12 col-form-label", @id = "" })
<div class="col-lg-8 col-md-8 col-sm-12 col-xs-12">
@Html.DropDownListFor(model => model.Year5, new SelectList(Model.YearList5, "Value", "Text"), new { @class = "form-control" })
</div>
<div class="col-lg-1 col-md-1 col-sm-1 col-xs-1"><span class="glyphicon glyphicon-remove removeYear" style="color:red; font-size:18px; top:8px"></span></div>
</div>
<div class="form-group row year" id="year6" style="display:none">
@Html.LabelFor(model => model.Year6, htmlAttributes: new { @class = "col-lg-3 col-md-3 col-sm-12 col-xs-12 col-form-label", @id = "" })
<div class="col-lg-8 col-md-8 col-sm-12 col-xs-12">
@Html.DropDownListFor(model => model.Year6, new SelectList(Model.YearList6, "Value", "Text"), new { @class = "form-control" })
</div>
<div class="col-lg-1 col-md-1 col-sm-1 col-xs-1"><span class="glyphicon glyphicon-remove removeYear" style="color:red; font-size:18px; top:8px"></span></div>
</div>
<div class="form-group row year" id="year7" style="display:none">
@Html.LabelFor(model => model.Year7, htmlAttributes: new { @class = "col-lg-3 col-md-3 col-sm-12 col-xs-12 col-form-label", @id = "" })
<div class="col-lg-8 col-md-8 col-sm-12 col-xs-12">
@Html.DropDownListFor(model => model.Year7, new SelectList(Model.YearList7, "Value", "Text"), new { @class = "form-control" })
</div>
<div class="col-lg-1 col-md-1 col-sm-1 col-xs-1"><span class="glyphicon glyphicon-remove removeYear" style="color:red; font-size:18px; top:8px"></span></div>
</div>
<div class="form-group row year" id="year8" style="display:none">
@Html.LabelFor(model => model.Year8, htmlAttributes: new { @class = "col-lg-3 col-md-3 col-sm-12 col-xs-12 col-form-label", @id = "" })
<div class="col-lg-8 col-md-8 col-sm-12 col-xs-12">
@Html.DropDownListFor(model => model.Year8, new SelectList(Model.YearList8, "Value", "Text"), new { @class = "form-control" })
</div>
<div class="col-lg-1 col-md-1 col-sm-1 col-xs-1"><span class="glyphicon glyphicon-remove removeYear" style="color:red; font-size:18px; top:8px"></span></div>
</div>
<div class="form-group m-b-0">
<button class="btn btn-info btn-success addYear" style="background: #00c292; border: 1px solid #00c292;">Add Option</button>
<button class="removeYear">Remove Year</button>
<button class="btn btn-info pull-right submit">Update Options</button>
</div>
</div>
</div>
@* } *@
</div>
<script>
if ("@Model.Name" != "GetPortfolioBenchmarksPerformance") {
$("div#year4").show();
$("div#year5").show();
};
$('button.addYear').on('click', function (e) {
e.preventDefault();
var lastVisibleDiv = $(".year:hidden:first");
var id = lastVisibleDiv.attr("id");
alert(id);
lastVisibleDiv.show();
// .addClass("show");
});
$('span.removeYear').on('click', function (e) {
e.preventDefault();
var lastVisibleDiv = $(this).parent().parent();
// alert(lastVisibleDiv.next(".form-group").attr("id"));
var id = lastVisibleDiv.attr("id");
lastVisibleDiv.hide();
// .addClass("show");
});
</script>
I have tried a few other selectors and have had no luck getting the correct one. Seems to always pull the id year1 div.
I need this to be able to remove them and when add clicked it readds the first one hidden on the page.
Thanks In advance