Get click value from unordered list
How can I get the value / Reason of the selected item? I want to store this value in a variable and throw it into sessionStorage for the next page. I believe there is a much better way to do all this, but I have no idea how, if there is, let me know please!
I am completely new to JS and would appreciate some general guidance on how to approach this. Thank.
$(".reasons_items").click(function() {
$(this).toggleClass("reasons_items_click");
});
ul.reasons_menu li.reasons_items {
width: 50%;
float: left;
text-align: center;
color: #fff;
padding: 5px 0;
border-top: 1px solid #596467;
border-right: 1px solid #596467;
border-left: 1px solid #596467;
border-bottom: 1px solid #596467;
height: 120px;
cursor: pointer;
background-color: #0192C9;
position: relative;
z-index: 1;
}
ul.reasons_menu li.reasons_items:hover,
ul.reasons_menu.reasons_items.reasons_items_click {
background: #fff;
color: #333333;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="reasons_menu">
<li class="reasons_top_item">
<h2>Why</h2>
</li>
<li class="reasons_items">
<p>Reason 1</p><i class="fa fa-database fa-3x"></i></li>
<li class="reasons_items">
<p>Reason 2</p><i class="fa fa-handshake-o fa-3x"></i></li>
<li class="reasons_items">
<p>Reason 3</p><i class="fa fa-users fa-3x"></i></li>
<li class="reasons_items">
<p>Reason 4</p><i class="fa fa-line-chart fa-3x"></i></li>
</ul>
source to share
Hi this is a different approach to solving your problem. Working fiddle: https://jsfiddle.net/ash06229/4mqbcgfx/
$(".reasons_items").click(function () {
$('.reasons_items').removeClass("reasons_items_click");
$(this).addClass("reasons_items_click");
$('.reasons_top_item h2').text($('.reasons_items_click p').text())
});
The expected value is reflected on the first li , having class prichinam_top_item .
source to share
Use . find () to get the text of the item p
for children and sessionStorage.setItem () to set it for the next page, for example
$(".reasons_items").click(function () {
sessionStorage.setItem('reason', //seeting key for sessionstorage
$(this).toggleClass("reasons_items_click") // toggle class
.find("p") // get p element
.text() // get text
);
});
source to share
You can just take the text from p
as others suggest. But you are creating an item that collects the item option
and so I suggest the same approach. Use data-value
to get the value of your reasons / parameters like this:
<li class="reasons_items" data-value="Reason1"><p>Reason 1</p><i class="fa fa-database fa-3x"></i></li>
source to share