Search code examples
javascriptcheckboxradio-buttonwindow.open

Radio and Checkbox Checked Values Won't Show in JavaScript


I'm trying to get the value of a radio button and multiple checkboxes to appear in a new window using JavaScript. I can't seem to figure out why "true" return instead of the actual value checked. Below is the code and screenshot of the results.

enter image description here

function messagebox()
{
	var newWindow;
	var msg ="";
	var i = document.PizzaForm.state.options.selectedIndex;
	var text = document.PizzaForm.state.options[i].text;
	var value = document.PizzaForm.state.options[i].value;
	
	{ document.PizzaForm.customer.value = document.PizzaForm.customer.value.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); }
	
	{ document.PizzaForm.city.value = document.PizzaForm.city.value.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); }
	
	newWindow = window.open("","","status=no,height=500,width=500");
	message = "<ul><li>Name: " + document.PizzaForm.customer.value;
    message += "<li>Address: " + document.PizzaForm.address.value;
    message += "<li>City: " + document.PizzaForm.city.value;
    message += "<li>State: " + document.PizzaForm.state.value;
    message += "<li>Zip Code: " + document.PizzaForm.zip.value;
    message += "<li>Phone: " + document.PizzaForm.phone.value;
    message += "<li>Email: " + document.PizzaForm.email.value;
	message += "<li>Size: " + showSize();
	message += "<li>Toppings: " + showToppings();
	newWindow.document.write(message);

function showSize()
	{
	for(i=0;i<document.PizzaForm.sizes.length;i++)
        if(document.PizzaForm.sizes[i].checked)
		msg += document.PizzaForm.sizes[i].value;
	return true;
	}
	
function showToppings()
	{
	for(i=0;i<document.PizzaForm.toppings.length;i++)
        if(document.PizzaForm.toppings[i].checked)
            msg += (i==0?"":",")+document.PizzaForm.toppings[i].value;
	return true;
	}	
}


Solution

  • the showSize() and the showToppings() methods are both returning true in the end which is why you're getting true in your string. you should return msg and your problem will be solved.