I'm doing 'Calendar Event' app. After choosing the day of the week, times and short description I can click on 'Add Event' button and add a row to table (not visible), but I don't know, how to add a cell to it.
I was trying to add cell, by grabbing '.event' class var eventContent = document.querySelectorAll('.event'); , and use appendChild() method,
for(var j = 0; j < eventContent.length; j +=1){
eventContent[j].appendChild(tdElement1);
eventContent[j].appendChild(tdElement2);
}
but the cells are not inserting as should be, after changing the day of the week.
There is the HTML code:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Daily Programmer Challenge #0004 | Events Calendar</title>
<link rel="stylesheet" href="css/0004_eventsCalendar.css">
</head>
<body>
<div id="container">
<div class="toolbarMenu">
<div class="toolLeft">
<div class="leftButtons">
<button type="button"><span><</span></button>
<button type="button"><span>></span></button>
</div>
<button type="button">today</button>
</div>
<div class="toolCenter">
<h2>Test</h2>
</div>
</div>
<hr>
<div id="content">
<div id="leftSide">
<table>
<tbody>
<tr class="eventHead">
<td class="dayHead" colspan="2"><span class="day">Monday</span><span class="dayDate"></span></td>
</tr>
<tr class="eventHead">
<td class="dayHead" colspan="2"><span class="day">Tuesday</span><span class="dayDate"></span></td>
</tr>
<tr class="eventHead">
<td class="dayHead" colspan="2"><span class="day">Wednesday</span><span class="dayDate"></span></td>
</tr>
<tr class="eventHead">
<td class="dayHead" colspan="2"><span class="day">Thursday</span><span class="dayDate"></span></td>
</tr>
<tr class="eventHead">
<td class="dayHead" colspan="2"><span class="day">Friday</span><span class="dayDate"></span></td>
</tr>
<tr class="eventHead">
<td class="dayHead" colspan="2"><span class="day">Saturday</span><span class="dayDate"></span></td>
</tr>
<tr class="eventHead">
<td class="dayHead" colspan="2"><span class="day">Sunday</span><span class="dayDate"></span></td>
</tr>
<!--
<tr class="event">
<td class="time"></td>
<td class="desc"></td>
</tr>
-->
</tbody>
</table>
<p><i><strong>Note:</strong> After adding event into table, you can EDIT it!</i></p>
</div>
<div id="rigthSide">
<h3>Calendar panel</h3>
<form id="calendarCMS" name = "calendarCMS" action="#">
<fieldset>
<select name="dayOfWeek" id="dayOfWeek">
<option value="monday">Monday</option>
<option value="tuesday">Tuesday</option>
<option value="wednesday">Wednesday</option>
<option value="thursday">Thursday</option>
<option value="friday">Friday</option>
<option value="saturday">Saturday</option>
<option value="sunday">Sunday</option>
</select>
<div id="fromToTime">
<span>from:</span>
<select name="fromTime" id="fromTime">
<option value="07.00am">07:00am</option>
<option value="07.15am">07:15am</option>
<option value="07.30am">07:30am</option>
<option value="07.45am">07:45am</option>
<option value="08.00am">08:00am</option>
<option value="08.15am">08:15am</option>
<option value="08.30am">08:30am</option>
<option value="08.45am">08:45am</option>
<option value="09.00am">09:00am</option>
<option value="09.15am">09:15am</option>
<option value="09.30am">09:30am</option>
<option value="09.45am">09:45am</option>
<option value="10.00am">10:00am</option>
<option value="10.15am">10:15am</option>
<option value="10.30am">10:30am</option>
<option value="10.45am">10:45am</option>
<option value="11.00am">11:00am</option>
<option value="11.15am">11:15am</option>
<option value="11.30am">11:30am</option>
<option value="11.45am">11:45am</option>
<option value="12.00am">12:00am</option>
<option value="12.15am">12:15am</option>
<option value="12.30am">12:30am</option>
<option value="12.45am">12:45am</option>
<option value="13.00pm">13:00pm</option>
<option value="13.15pm">13:15pm</option>
<option value="13.30pm">13:30pm</option>
<option value="13.45pm">13:45pm</option>
<option value="14.00pm">14:00pm</option>
<option value="14.15pm">14:15pm</option>
<option value="14.30pm">14:30pm</option>
<option value="14.45pm">14:45pm</option>
<option value="15.00pm">15:00pm</option>
<option value="15.15pm">15:15pm</option>
<option value="15.30pm">15:30pm</option>
<option value="15.45pm">15:45pm</option>
<option value="16.00pm">16:00pm</option>
<option value="16.15pm">16:15pm</option>
<option value="16.30pm">16:30pm</option>
<option value="16.45pm">16:45pm</option>
<option value="17.00pm">17:00pm</option>
<option value="17.15pm">17:15pm</option>
<option value="17.30pm">17:30pm</option>
<option value="17.45pm">17:45pm</option>
<option value="18.00pm">18:00pm</option>
<option value="18.15pm">18:15pm</option>
<option value="18.30pm">18:30pm</option>
<option value="18.45pm">18:45pm</option>
<option value="19.00pm">19:00pm</option>
<option value="19.15pm">19:15pm</option>
<option value="19.30pm">19:30pm</option>
<option value="19.45pm">19:45pm</option>
<option value="20.00pm">20:00pm</option>
<option value="20.15pm">20:15pm</option>
<option value="20.30pm">20:30pm</option>
<option value="20.45pm">20:45pm</option>
<option value="21.00pm">21:00pm</option>
<option value="21.15pm">21:15pm</option>
<option value="21.30pm">21:30pm</option>
<option value="21.45pm">21:45pm</option>
<option value="22.00pm">22:00pm</option>
<option value="22.15pm">22:15pm</option>
<option value="22.30pm">22:30pm</option>
<option value="22.45pm">22:45pm</option>
<option value="23.00pm">23:00pm</option>
<option value="23.15pm">23:15pm</option>
<option value="23.30pm">23:30pm</option>
<option value="23.45pm">23:45pm</option>
<option value="00.00am">00:00am</option>
<option value="00.15am">00:15am</option>
<option value="00.30am">00:30am</option>
<option value="00.45am">00:45am</option>
<option value="01.00am">01:00am</option>
<option value="01.15am">01:15am</option>
<option value="01.30am">01:30am</option>
<option value="01.45am">01:45am</option>
<option value="02.00am">02:00am</option>
<option value="02.15am">02:15am</option>
<option value="02.30am">02:30am</option>
<option value="02.45am">02:45am</option>
<option value="03.00am">03:00am</option>
<option value="03.15am">03:15am</option>
<option value="03.30am">03:30am</option>
<option value="03.45am">03:45am</option>
<option value="04.00am">04:00am</option>
<option value="04.15am">04:15am</option>
<option value="04.30am">04:30am</option>
<option value="04.45am">04:45am</option>
<option value="05.00am">05:00am</option>
<option value="05.15am">05:15am</option>
<option value="05.30am">05:30am</option>
<option value="05.45am">05:45am</option>
<option value="06.00am">06:00am</option>
<option value="06.15am">06:15am</option>
<option value="06.30am">06:30am</option>
<option value="06.45am">06:45am</option>
</select>
<span>to:</span>
<select name="toTime" id="toTime">
<option value="07.00am">07:00am</option>
<option value="07.15am">07:15am</option>
<option value="07.30am">07:30am</option>
<option value="07.45am">07:45am</option>
<option value="08.00am">08:00am</option>
<option value="08.15am">08:15am</option>
<option value="08.30am">08:30am</option>
<option value="08.45am">08:45am</option>
<option value="09.00am">09:00am</option>
<option value="09.15am">09:15am</option>
<option value="09.30am">09:30am</option>
<option value="09.45am">09:45am</option>
<option value="10.00am">10:00am</option>
<option value="10.15am">10:15am</option>
<option value="10.30am">10:30am</option>
<option value="10.45am">10:45am</option>
<option value="11.00am">11:00am</option>
<option value="11.15am">11:15am</option>
<option value="11.30am">11:30am</option>
<option value="11.45am">11:45am</option>
<option value="12.00am">12:00am</option>
<option value="12.15am">12:15am</option>
<option value="12.30am">12:30am</option>
<option value="12.45am">12:45am</option>
<option value="13.00pm">13:00pm</option>
<option value="13.15pm">13:15pm</option>
<option value="13.30pm">13:30pm</option>
<option value="13.45pm">13:45pm</option>
<option value="14.00pm">14:00pm</option>
<option value="14.15pm">14:15pm</option>
<option value="14.30pm">14:30pm</option>
<option value="14.45pm">14:45pm</option>
<option value="15.00pm">15:00pm</option>
<option value="15.15pm">15:15pm</option>
<option value="15.30pm">15:30pm</option>
<option value="15.45pm">15:45pm</option>
<option value="16.00pm">16:00pm</option>
<option value="16.15pm">16:15pm</option>
<option value="16.30pm">16:30pm</option>
<option value="16.45pm">16:45pm</option>
<option value="17.00pm">17:00pm</option>
<option value="17.15pm">17:15pm</option>
<option value="17.30pm">17:30pm</option>
<option value="17.45pm">17:45pm</option>
<option value="18.00pm">18:00pm</option>
<option value="18.15pm">18:15pm</option>
<option value="18.30pm">18:30pm</option>
<option value="18.45pm">18:45pm</option>
<option value="19.00pm">19:00pm</option>
<option value="19.15pm">19:15pm</option>
<option value="19.30pm">19:30pm</option>
<option value="19.45pm">19:45pm</option>
<option value="20.00pm">20:00pm</option>
<option value="20.15pm">20:15pm</option>
<option value="20.30pm">20:30pm</option>
<option value="20.45pm">20:45pm</option>
<option value="21.00pm">21:00pm</option>
<option value="21.15pm">21:15pm</option>
<option value="21.30pm">21:30pm</option>
<option value="21.45pm">21:45pm</option>
<option value="22.00pm">22:00pm</option>
<option value="22.15pm">22:15pm</option>
<option value="22.30pm">22:30pm</option>
<option value="22.45pm">22:45pm</option>
<option value="23.00pm">23:00pm</option>
<option value="23.15pm">23:15pm</option>
<option value="23.30pm">23:30pm</option>
<option value="23.45pm">23:45pm</option>
<option value="00.00am">00:00am</option>
<option value="00.15am">00:15am</option>
<option value="00.30am">00:30am</option>
<option value="00.45am">00:45am</option>
<option value="01.00am">01:00am</option>
<option value="01.15am">01:15am</option>
<option value="01.30am">01:30am</option>
<option value="01.45am">01:45am</option>
<option value="02.00am">02:00am</option>
<option value="02.15am">02:15am</option>
<option value="02.30am">02:30am</option>
<option value="02.45am">02:45am</option>
<option value="03.00am">03:00am</option>
<option value="03.15am">03:15am</option>
<option value="03.30am">03:30am</option>
<option value="03.45am">03:45am</option>
<option value="04.00am">04:00am</option>
<option value="04.15am">04:15am</option>
<option value="04.30am">04:30am</option>
<option value="04.45am">04:45am</option>
<option value="05.00am">05:00am</option>
<option value="05.15am">05:15am</option>
<option value="05.30am">05:30am</option>
<option value="05.45am">05:45am</option>
<option value="06.00am">06:00am</option>
<option value="06.15am">06:15am</option>
<option value="06.30am">06:30am</option>
<option value="06.45am">06:45am</option>
</select>
</div>
<textarea name="eventDesc" id="eventDesc" cols="45" rows="10" placeholder="Please write the description event details"></textarea>
<input type="submit" value="Add Event">
</fieldset>
<div id="error"></div>
</form>
</div>
</div>
</div>
<script src="js/0004_eventsCalendar.js"></script>
</body>
</html>
JavaScript code:
//self-executing function to protect our local variables
(function(){
// current date
var curentDate = new Date();
// set variable to first day of week (Monday)
var firstDay = (curentDate.getDate() - (curentDate.getDay()-1));
// array of months
var allMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
// loop through to get last day (Friday) of the curent week
for(var i = 0; i < 7; i +=1){
var lastDay = firstDay + i;
}
// add the date to DOM element (h2 tag)
document.querySelector('h2').innerHTML =
allMonths[curentDate.getMonth()] + ' ' + firstDay + ' - ' + lastDay + ', ' + curentDate.getFullYear();
// get all DOM nodes with the class name ('.dayDate')
var dayDate = document.querySelectorAll('.dayDate');
// loop through those nodes and add to DOM month, current date and year
for(var i = 0; i < dayDate.length; i+=1){
dayDate[i].innerHTML = allMonths[curentDate.getMonth()] + ' ' + (firstDay + i) + ', ' + curentDate.getFullYear();
}
// assign form HTML element of Calendar panel to variables. Where '.fromTime' and '.toTome' , are names of <select> tag
var fromTime = document.calendarCMS.fromTime;
var toTime = document.calendarCMS.toTime;
// function for manipulating times (from: and to:), in Calendar panel
function changeFromTime(e){
// triger when time in first pop-down menu '.fromTime' is higher than in second pop-down menu '.toTime'
// where selectedIndex is a number (position) of currently selected element in pop-down menu
if(fromTime.selectedIndex >= toTime.selectedIndex){
// loop throogh first pop-down menu 'fromTime', where fromTime.selectedIndex is index curently selected element
for(var i = 0; i <= fromTime.selectedIndex; i +=1){
// hide all times in second pop-down menu 'toTime' which are below times in '.fromTime' pop-down menu
toTime[i].style.display = 'none';
}
// change position '.toTime' pop-down menu index to be equal to first pop-down menu '.froTime'
return toTime.selectedIndex = i;
}
// triger when time in first pop-down menu '.fromTime' is lower than in second pop-down menu '.toTime'
if(fromTime.selectedIndex <= toTime.selectedIndex){
// reverse (remove) styling when user change to earlier time, where we removing styling only to elements which are
// below curently selected index (i = fromTime.selectedIndex) in '.fromTime' pop-down menu
for(var i = fromTime.selectedIndex; i < toTime.selectedIndex; i +=1){
toTime[i].removeAttribute('style');
}
// change position '.toTime' pop-down menu index to be equal to first pop-down menu '.froTime'
return toTime.selectedIndex = fromTime.selectedIndex;
}
}
// function use to add/remove calendar events
function addEvent(e){
//event will triger only when user press submit 'Add Event' button
if(e.target.type === 'submit'){
// prevent submit button from default behavior
e.preventDefault();
// assign a couple of variables
var textArea = document.querySelector('textarea');
var eventHead = document.querySelectorAll('.eventHead');
var trElement = document.createElement('tr');
trElement.className = 'event';
var eventDesc = document.querySelectorAll('.desc');
var eventTime = document.querySelectorAll('.time');
var errMsg = document.querySelector('#error');
errMsg.style.color = "red";
// loop through description field in HTML table
for(var i = 0; i < eventHead.length; i+=1){
// trigger when option in index in 'dayOfWeek' pop-down menu will match index of description field
if(document.calendarCMS.dayOfWeek.selectedIndex === i){
// if textarea in HTML will be empty, then the error msg will pop out, blocking user from adding event
if(textArea.value === ''){
// error msg show when user doesn't fill all necessary details
errMsg.innerHTML = 'Please fill all fields';
return;
} else{ // if all require filed will be filled
/*
// after description of event will be added to table, then will be editable
eventDesc[i].contentEditable = true;
// after times of event will be added to table, then will be editable
eventTime[i].contentEditable = true;
*/
eventHead[i].parentNode.insertBefore(trElement.cloneNode(true), eventHead[i].nextElementSibling);
var eventContent = document.querySelectorAll('.event');
var tdElement1 = document.createElement('td');
var tdElement2 = document.createElement('td');
tdElement1.className = 'time';
tdElement2.className = 'desc';
for(var j = 0; j < eventContent.length; j +=1){
eventContent[j].appendChild(tdElement1);
eventContent[j].appendChild(tdElement2);
}
// reset all field in Calendar panel
fromTime.selectedIndex = 0;
toTime.selectedIndex = 0;
textArea.value = '';
errMsg.innerHTML = '';
}
}
}
}
}
// function for higlight current day of the week
function todayDay(e){
// triger only when user click 'today' button
if(e.target.textContent === 'today'){
var focusEvent = document.querySelectorAll('.event');
// loop through to find the current day of the week and highliht it
for(var i = 0; i < focusEvent.length; i +=1){
if(curentDate.getDay() === i){
focusEvent[i-1].style.background = 'yellow';
}
}
}
}
// events listeners
document.querySelector('#calendarCMS').addEventListener('click', addEvent, false);
document.querySelector('#fromTime').addEventListener('change', changeFromTime, false);
document.querySelector('.toolLeft').addEventListener('click', todayDay, false);
})();
I do not exactly apprehend your attempt with the commented tr class "event". But what you want to achieve (at least inferring from the description) can be done by declaring addEvent as :
function addEvent(e){
//event will triger only when user press submit 'Add Event' button
if(e.target.type === 'submit'){
// prevent submit button from default behavior
e.preventDefault();
// assign a couple of variables
var textArea = document.querySelector('textarea');
var eventHead = document.querySelectorAll('.eventHead');
var trElement = document.createElement('tr');
trElement.className = 'event';
var eventDesc = document.querySelectorAll('.desc');
var eventTime = document.querySelectorAll('.time');
var errMsg = document.querySelector('#error');
errMsg.style.color = "red";
// loop through description field in HTML table
for(var i = 0; i < eventHead.length; i+=1){
// trigger when option in index in 'dayOfWeek' pop-down menu will match index of description field
if(document.calendarCMS.dayOfWeek.selectedIndex === i){
// if textarea in HTML will be empty, then the error msg will pop out, blocking user from adding event
if(textArea.value === ''){
// error msg show when user doesn't fill all necessary details
errMsg.innerHTML = 'Please fill all fields';
return;
} else{ // if all require filed will be filled
/*
// after description of event will be added to table, then will be editable
eventDesc[i].contentEditable = true;
// after times of event will be added to table, then will be editable
eventTime[i].contentEditable = true;
*/
var newTr = trElement.cloneNode(true);
eventHead[i].parentNode.insertBefore(newTr, eventHead[i].nextElementSibling);
var eventContent = document.querySelectorAll('.event');
var tdElement1 = document.createElement('td');
var tdElement2 = document.createElement('td');
tdElement1.innerText=fromTime.value + " - " + toTime.value;
tdElement2.innerText=textArea.value;
tdElement1.className = 'time';
tdElement2.className = 'desc';
for(var j = 0; j < eventContent.length; j +=1){
newTr.appendChild(tdElement1);
newTr.appendChild(tdElement2);
}
// reset all field in Calendar panel
fromTime.selectedIndex = 0;
toTime.selectedIndex = 0;
textArea.value = '';
errMsg.innerHTML = '';
}
}
}
}
}
You can try it here : https://jsfiddle.net/dn9v5qf9/
Related
I want to make a select option for size that depends on what the person chooses on a previous category select. Basically, if the person chooses clothing category then the next select will only show sizes (S,M,L,XL) and if he chooses shoes he will only be shown (33,34,44,42) you get the idea..
I use nodeJS and EJS as my template. I am NOT familiar with Jquery.
my code:
<select id='inputStyle' name='category'>
<option disabled='true'>Category</option>
<option value='shoes'>Championes</option>
<option value='shirts'>Camiseta</option>
<option value=''>sweatshirts</option>
<option value='others'>Otro</option>
</select>
</div>
<div class='inputStyle'>
<label for='size'> Size </label>
<br>
<select id='size' name='size'>
<option value=''></option>
<option value=''></option>
<option value=''></option>
<option value='others'></option>
</select>
You can do it by just using the below code. Just replace the list according to your needs.
var sizeForShoes = ["40", "44", "42", "46", "50"];
var sizeForDress = ["S", "M", "L", "XL", "XXL"];
$('#Category').change(function() {
var selectedCategory = $('#Category').val();
if(selectedCategory != ""){
//Removing current option
$('#size').find('option').remove();
var sizeList = [];
if(selectedCategory == 'shoes'){
for (var i = 1; i <= sizeForShoes.length; i++) {
var shoeSize = sizeForShoes[i];
$('#size').append($("<option></option>").attr("value", shoeSize).text(shoeSize));
}
}
else{
for (var i = 1; i <= sizeForDress.length; i++) {
var dressSize = sizeForDress[i];
$('#size').append($("<option></option>").attr("value", dressSize).text(dressSize));
}
}
}else{
//If nothing is selected then it will remove previous options
$("#size").empty();
$('#size').append($("<option></option>").attr("value", "").text("Select size"));
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="Category">Category</label>
<select id="Category" name="Category" >
<option value="">Select Category</option>
<option value='shoes'>Championes</option>
<option value='shirts'>Camiseta</option>
<option value='sweatshirts'>sweatshirts</option>
<option value='others'>Otro</option>
</select><br><br>
<label for="size">Size</label>
<select id="size" name="size">
<option value="">Select size</option>
</select>
My goal is to remove options from a select input ("class") based on their classList depending on the selection made in a previous select input ("class-type"). Strangely enough, some options ARE removed (as they should be) when enableClassSelect is called, but not all options which don't have the necessary class are moved at once. Currently the "class-list" needs to be changed ~7 times before ALL options are removed (this should not happen!).
See code below - I've reviewed it several times and attempted some debugging to no avail. console.log(classSelect.length); prints "46", which is correct, so each option element should be checked in the iteration.
function enableClassSelect(classType) {
let classSelect = document.getElementById("class");
console.log("Length: " + classSelect.length);
for (i = 0; i < classSelect.length; i++) {
if (!classSelect.options[i].classList.contains(classType)) {
classSelect.remove(i);
}
}
classSelect.disabled = false;
classSelect.classList.remove("disabled-select");
let classDefaultOption = document.getElementById("class-default-option");
classDefaultOption.innerHTML = " -- select a class -- ";
return;
}
<select onchange="javascript:enableClassSelect(this.options[this.selectedIndex].value);" class="reg" id="class-type" name="class-type" required>
<option disabled selected value> -- select a class type -- </option>
<option value="Magician">Magician</option>
<option value="Thief">Thief</option>
<option value="Warrior">Warrior</option>
<option value="Bowman">Bowman</option>
<option value="Pirate">Pirate</option>
</select>
<select class="disabled-select reg" id="class" name="class" disabled required>
<option class="Magician Thief Warrior Bowman Pirate" id="class-default-option" disabled selected value></option>
<!-- Magicians -->
<option class="Magician" value="Battle Mage">Battle Mage</option>
<option class="Magician" value="Beast Tamer">Beast Tamer</option>
<option class="Magician" value="Blaze Wizard">Blaze Wizard</option>
<option class="Magician" value="Evan">Evan</option>
<option class="Magician" value="Kanna">Kanna</option>
<option class="Magician" value="Luminous">Luminous</option>
<option class="Magician" value="Bishop">Bishop</option>
<option class="Magician" value="Ice/Lightning Mage">Ice/Lightning Mage</option>
<option class="Magician" value="Fire/Poison Mage">Fire/Poison Mage</option>
<option class="Magician" value="Kinesis">Kinesis</option>
<option class="Magician" value="Illium">Illium</option>
<!-- Thieves -->
<option class="Thief" value="Dual Blade">Dual Blade</option>
<option class="Thief" value="Night Walker">Night Walker</option>
<option class="Thief" value="Phantom">Phantom</option>
<option class="Thief" value="Shadower">Shadower</option>
<option class="Thief" value="Night Lord">Night Lord</option>
<option class="Thief" value="Xenon">Xenon</option>
<option class="Thief" value="Cadena">Cadena</option>
<!-- Warriors -->
<option class="Warrior" value="Aran">Aran</option>
<option class="Warrior" value="Dawn Warrior">Dawn Warrior</option>
<option class="Warrior" value="Demon Avenger">Demon Avenger</option>
<option class="Warrior" value="Demon Slayer">Demon Slayer</option>
<option class="Warrior" value="Hayato">Hayato</option>
<option class="Warrior" value="Kaiser">Kaiser</option>
<option class="Warrior" value="Mihile">Mihile</option>
<option class="Warrior" value="Dark Knight">Dark Knight</option>
<option class="Warrior" value="Hero">Hero</option>
<option class="Warrior" value="Paladin">Paladin</option>
<option class="Warrior" value="Zero">Zero</option>
<option class="Warrior" value="Blaster">Blaster</option>
<!-- Bowmen -->
<option class="Bowman" value="Marksman">Marksman</option>
<option class="Bowman" value="Bowmaster">Bowmaster</option>
<option class="Bowman" value="Wild Hunter">Wild Hunter</option>
<option class="Bowman" value="Wind Archer">Wind Archer</option>
<option class="Bowman" value="Mercedes">Mercedes</option>
<option class="Bowman" value="Pathfinder">Pathfinder</option>
<!-- Pirates -->
<option class="Pirate" value="Angelic Buster">Angelic Buster</option>
<option class="Pirate" value="Cannoneer">Cannoneer</option>
<option class="Pirate" value="Jett">Jett</option>
<option class="Pirate" value="Mechanic">Mechanic</option>
<option class="Pirate" value="Buccaneer">Buccaneer</option>
<option class="Pirate" value="Corsair">Corsair</option>
<option class="Pirate" value="Shade">Shade</option>
<option class="Pirate" value="Thunder Breaker">Thunder Breaker</option>
<option class="Pirate" value="Ark">Ark</option>
</select>
Expected: select a class type (e.g. "Warrior" and the "class" element becomes enabled and displays the proper classes.
Actual: "class" options are removed seemingly at random over ~6 calls to enableClassSelect until the select element is empty.
This is one way of doing it, not sure if it would be the fastest way, but it does work.
Essentially I removed all of the options from the HTML, and instead I am only adding on the correct options for that class type.
function enableClassSelect(classType) {
// Make sure the names of these are the same as the value for the options in the class-type select
const magicians = ["Battle Mage", "Beast Tamer", "Blaze Wizard"];
const thieves = ["Dual Blade", "Night Walker", "Phantom"];
// Make sure theses are both in the same order
const classTypesNames = ["magicians", "thieves"];
const classTypes = [magicians, thieves];
let classSelect = document.getElementById("class");
// Clear the options from the classSelect, except for the default option
var length = classSelect.options.length;
if (length > 1) {
for (i = classSelect.options.length - 1; i > 0; i--) {
classSelect.remove(i);
}
}
// Check the classtype they selected, and get the list of classes for that classType
for (i = 0; i < classTypes.length; i++) {
if (classType === classTypesNames[i]) {
var classes = classTypes[i];
for (j = 0; j < classes.length; j++) {
// Create an option element with the class as the value
var opt = document.createElement("option");
opt.value = classes[j];
opt.innerHTML = classes[j];
classSelect.appendChild(opt);
}
}
}
classSelect.disabled = false;
classSelect.classList.remove("disabled-select");
return;
}
<select onchange="javascript:enableClassSelect(this.options[this.selectedIndex].value);" class="reg" id="class-type" name="class-type" required>
<option disabled selected value> -- select a class type -- </option>
<option value="magicians">Magician</option>
<option value="thieves">Thief</option>
</select>
<select class="disabled-select reg" id="class" name="class" disabled required>
<option id="class-default-option" disabled selected value> -- select a class -- </option>
</select>
There needs to be a list for each classType that contains the classes for that classType.
const warriors = ["Aran", "Dawn Warrior", "Demon Avenger"];
Then add that classType to the list of classTypes:
const classTypesNames = ["magicians", "thieves", "warriors"];
const classTypes = [magicians, thieves, warriors];
This part "resets" the options for the classSelect so that only the default one remains:
var length = classSelect.options.length;
if (length > 1) {
for (i = classSelect.options.length - 1; i > 0; i--) {
classSelect.remove(i);
}
}
From there the next section checks the value of the classType the user selected, and gets the list of classes for that classType, and creates option elements and appends them to the classSelect:
for (i = 0; i < classTypes.length; i++) {
if (classType === classTypesNames[i]) {
var classes = classTypes[i];
for (j = 0; j < classes.length; j++) {
// Create an option element with the class as the value
var opt = document.createElement("option");
opt.value = classes[j];
opt.innerHTML = classes[j];
classSelect.appendChild(opt);
}
}
}
This question already has answers here:
How to get all selected values of a multiple select box?
(28 answers)
Closed 5 years ago.
I'm still a beginner hence this is difficult, but how do I display the options I selected into an alert box. So it would be "You selected (value), (value), (value)".
This is my select list
<form id='form1'>
<select id="options" multiple >
<option value="action">Action</option>
<option value="animation">Animation</option>
<option value="children">Children</option>
<option value="classics">Classics</option>
<option value="comedy">Comedy</option>
<option value="documentary">Documentary</option>
<option value="drama">Drama</option>
<option value="family">Family</option>
<option value="foreign">Foreign</option>
<option value="games">Games</option>
<option value="horror">Horror</option>
<option value="music">Music</option>
<option value="new">New</option>
<option value="scifi">Sci-Fi</option>
<option value="sports">Sports</option>
<option value="travel">Travel</option>
</select>
</form>
Should I add a button. But what I'm struggling with is the javascript.
The easiest way to access selected elements of a select tag is with the "selectedOptions" property.
I would do it this way:
var form = document.getElementById('form1');
form.addEventListener('submit', function () {
var select = form.querySelector('#options'),
options = select.selectedOptions,
values = [];
for (var i = options.length - 1; i >= 0; i--) {
values.push(options[i].value);
}
alert('You selected: ' + values.join(', '));
}, false);
Try something like this:
function selectedValues()
{
var x=document.getElementById("options");
var selectedValues= '';
for (var i = 0; i < x.options.length; i++) {
if(x.options[i].selected ==true){
selectedValues += x.options[i].value + ", ";
}
}
alert("You selected: "+ selectedValues.slice(0, -2));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id='form1'>
<select id="options" multiple onchange="selectedValues()">
<option value="action">Action</option>
<option value="animation">Animation</option>
<option value="children">Children</option>
<option value="classics">Classics</option>
<option value="comedy">Comedy</option>
<option value="documentary">Documentary</option>
<option value="drama">Drama</option>
<option value="family">Family</option>
<option value="foreign">Foreign</option>
<option value="games">Games</option>
<option value="horror">Horror</option>
<option value="music">Music</option>
<option value="new">New</option>
<option value="scifi">Sci-Fi</option>
<option value="sports">Sports</option>
<option value="travel">Travel</option>
</select>
</form>
When the Primary dropdown changes, I want the other dropdown to change accordingly.
But I dont know why only the 1st Other dropdown is working while the 2nd fails to change its value accordingly. I
m aware that if I change the name of the 2nd dropdown to the same as the 1st dropdown it will work.
But as they are different fields that are to be saved in the db, so the names have to be different.
Any solution would be appreciated. Many thanks.
function setDropDown() {
var index_name =
document.getElementsByName('ForceSelection')[0].selectedIndex;
var others = document.getElementsByName('Qualifications');
for (i = 0; i < others.length; i++)
others[i].selectedIndex = index_name;
var others2 = document.getElementsByName('Qualifications2');
for (i = 0; i < others2.length; i++)
others2[i].selectedIndex = index_name2;
}
Primary dropdown<select name="ForceSelection" id="ForceSelection" onChange="javascript:return setDropDown();">
<option value="" selected>Select</option>
<option value="treatmentid1">treatmentname1</option>
<option value="treatmentid2">treatmentname2</option>
</select> other dropdown
<select id="Qualifications" name="Qualifications">
<option value="select">select</option>
<option value="treatmentid1">treatmentname1</option>
<option value="treatmentid2">treatmentname2</option>
</select> other dropdown2
<select id="Qualifications2" name="Qualifications2">
<option value="select">select</option>
<option value="treatmentid1">treatmentname1</option>
<option value="treatmentid2">treatmentname2</option>
</select>
Add a css class to your secondary drop downs and use document.querySelectorAll to get them all at once.
Then you can use a single loop to update their selectedIndex.
function setDropDown() {
var index_name =
document.getElementsByName('ForceSelection')[0].selectedIndex;
var others = document.querySelectorAll('.secondary');
for (var i = 0; i < others.length; i++) {
others[i].selectedIndex = index_name;
}
}
div {
padding: 15px;
}
<div><b>Primary dropdown:</b>
<select name="ForceSelection" id="ForceSelection" onChange="javascript:return setDropDown();">
<option value="" selected>Select</option>
<option value="treatmentid1">treatmentname1</option>
<option value="treatmentid2">treatmentname2</option>
</select>
</div>
<div>
<b>Other dropdown 1</b>:
<select class='secondary' id="Qualifications" name="Qualifications">
<option value="select">select</option>
<option value="treatmentid1">treatmentname1</option>
<option value="treatmentid2">treatmentname2</option>
</select></div>
<div> <b>Other dropdown 1</b>:
<select class='secondary' id="Qualifications2" name="Qualifications2">
<option value="select">select</option>
<option value="treatmentid1">treatmentname1</option>
<option value="treatmentid2">treatmentname2</option>
</select>
</div>
So it turns out i haven't defined sth in my code, thanks for pointing that out #Tiny Giant
function setDropDown() {
var index_name =
document.getElementsByName('ForceSelection')[0].selectedIndex;
var index_name2 =
document.getElementsByName('ForceSelection')[0].selectedIndex;
var others = document.getElementsByName('Qualifications');
for (i = 0; i < others.length; i++)
others[i].selectedIndex = index_name;
var others2 = document.getElementsByName('Qualifications2');
for (j = 0; j < others2.length; j++)
others2[j].selectedIndex = index_name2;
}
Primary dropdown<select name="ForceSelection" id="ForceSelection" onChange="javascript:return setDropDown();">
<option value="" selected>Select</option>
<option value="treatmentid1">treatmentname1</option>
<option value="treatmentid2">treatmentname2</option>
</select> other dropdown
<select id="Qualifications" name="Qualifications">
<option value="select">select</option>
<option value="treatmentid1">treatmentname1</option>
<option value="treatmentid2">treatmentname2</option>
</select> other dropdown2
<select id="Qualifications2" name="Qualifications2">
<option value="select">select</option>
<option value="treatmentid1">treatmentname1</option>
<option value="treatmentid2">treatmentname2</option>
</select>
I want to get the Hour and minute value from below control by using there label
also I want to set the value to that control.
Using jQuery and javascript.
<td class="ms-dttimeinput" nowrap="nowrap">
<label for="ctl00_m_g_460bfeda_4d7a_4f5e_9587_9e0cd11d65ce_ff211_ctl00_ctl00_DateTimeField_DateTimeFieldDateHours" style="display:none">NEndTime Hours</label>
<select name="ctl00$m$g_460bfeda_4d7a_4f5e_9587_9e0cd11d65ce$ff211$ctl00$ctl00$DateTimeField$DateTimeFieldDateHours" id="ctl00_m_g_460bfeda_4d7a_4f5e_9587_9e0cd11d65ce_ff211_ctl00_ctl00_DateTimeField_DateTimeFieldDateHours">
<option selected="selected" value="00:">00:</option>
<option value="01:">01:</option>
<option value="21:">21:</option>
<option value="22:">22:</option>
<option value="23:">23:</option>
</select>
<label for="ctl00_m_g_460bfeda_4d7a_4f5e_9587_9e0cd11d65ce_ff211_ctl00_ctl00_DateTimeField_DateTimeFieldDateMinutes" style="display:none">NEndTime Minutes</label>
<select name="ctl00$m$g_460bfeda_4d7a_4f5e_9587_9e0cd11d65ce$ff211$ctl00$ctl00$DateTimeField$DateTimeFieldDateMinutes" id="ctl00_m_g_460bfeda_4d7a_4f5e_9587_9e0cd11d65ce_ff211_ctl00_ctl00_DateTimeField_DateTimeFieldDateMinutes">
<option selected="selected" value="00">00</option>
<option value="05">05</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
<option value="55">55</option>
</select></td>
Get the value for hr and min using the text in the label tags
var hr,min;
$(".ms-dttimeinput label").each(function() {
var lbl = $(this);
if(lbl.text() == "NEndTime Hours")
hr = lbl.next("select").val();
if(lbl.text() == "NEndTime Minutes")
min = lbl.next("select").val();
});
Set the value for hr and min using the text in the label tags
var hr = "01";
var min = "o5";
$(".ms-dttimeinput label").each(function() {
var lbl = $(this);
if(lbl.text() == "NEndTime Hours")
lbl.next("select").val(hr);
if(lbl.text() == "NEndTime Minutes")
lbl.next("select").val(min);
});
If there is only one widget on the page the more practical approach would be using the class name selector "ms-dttimeinput" with the descendent tag selector " select" and the nth child class ":nth-child()".
var hr = $(".ms-dttimeinput select:nth-child(1)").val();
var min = $(".ms-dttimeinput select:nth-child(2)").val();
$(".ms-dttimeinput select:nth-child(1)").val(hr);
$(".ms-dttimeinput select:nth-child(2)").val(min);