2 Replies - 559 Views - Last Post: 21 March 2013 - 05:13 AM

#1 carmex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 21-February 13

selected list explanation

Posted 21 March 2013 - 04:09 AM

Hi Dormilich,
was just wondering if you could explain to me how this function works:
		
function getSelectedList(actionselected) {
			
			// get selected list
			var obj = document.forms[0].elements;
			var selectedList = '';
			for (var i = 0; i < obj.length; i++) {
				if (obj[i].checked && obj[i].name == 'seats[]') {
					selectedList += obj[i].value + ', ';
				}
			}


I understand it is getting a list from the seats array. But whats the var obj = document.forms[0].elements for? I understand the for loop, that its checking the object length against "i" to make sure its all iterated through. However what is the obj.checked ? is that a built in part.
Just some further clarification would be great!

Is This A Good Question/Topic? 0
  • +

Replies To: selected list explanation

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3555
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: selected list explanation

Posted 21 March 2013 - 04:59 AM

// donít know what that param is	
function getSelectedList(actionselected) {
			
	// get all form elements
	var obj = document.forms[0].elements;
	// set var as (empty) string
	var selectedList = '';
	// loop through all form elements
	for (var i = 0; i < obj.length; i++) {
		// if the form element is checked (i.e. a check box or radio button)
		// and if the name is "seats[]"
		if (obj[i].checked && obj[i].name == 'seats[]') {
			// add the value of that element to the string
			// and append ", "
			selectedList += obj[i].value + ', ';
		}
	}
}

if you ask me, quite the complicated way to do that.
var seats = document.getElementsByName("seats[]");
var selectedList = Array.prototype.filter.call(seats, function(item) {
    return !!item.checked;
}).join(", ");

Was This Post Helpful? 1
  • +
  • -

#3 carmex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 21-February 13

Re: selected list explanation

Posted 21 March 2013 - 05:13 AM

Action selected refers to whether its being confirmed or canceled. (which button was pressed)
 
			function confirmReservation() {
			
			var selectedList = getSelectedList('Confirm Reservation');
			
			if (selectedList) {
				if (confirm('Do you want to CONFIRM this Reservation :  ' + selectedList + '?')) { 
					document.forms[0].statusA.value=0;
					document.forms[0].statusB.value=1;
					document.forms[0].previousPage.value='schoolHall';
					document.forms[0].action='bookingQueries.php';
					document.forms[0].submit();
				} else {
					clearSelection();
				}
			}
		}


for cancelreservation() StatusA=1 and statusB=0
which then makes the next code snippet work whichever action is being performed:
$changeStatus = "UPDATE $previousPage set status=$statusB, firstName='$fName', lastName='$lName' where( "; // UPDATE 



Quote

if you ask me, quite the complicated way to do that.
var seats = document.getElementsByName("seats[]");
var selectedList = Array.prototype.filter.call(seats, function(item) {
    return !!item.checked;
}).join(", ");

which is why i don't think i can implement this ^ even though i would like too

This post has been edited by carmex: 21 March 2013 - 05:16 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1