How can I take the selected values and insert them into two separate text fields?

Here's the code:

<!DOCTYPE html>
<html lang=en><head>
<title>Get Checks and ADD</title>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
<style>
body{background:#fed;}

#avail1 {
font-family: serif;
width: 48em;
padding: 5px;
border: 1px dotted #00b;
}

#avail1 input {
background: #99d;
margin-right:25px;
margin-bottom: 38px;
vertical-align: top;
}

#avail1 span {
display: inline-block;
width: 33em;
vertical-align: top;
}

#avail1 button {
margin: 7px 42px;
vertical-align: top;
}

#ordered {
margin-top:3px;
width: 48em;
padding: 5px;
border: 1px dotted #b00;
}
</style>
</head>
<body>
<h3>Get Checks and ADD</h3>
Order Online by Checking the Article below<br />
$10.00 per Article or $25.00 for Three, or $60.00 for all Nine Articles.
<p id="avail1">
<input type="checkbox" /><span>1 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<input type="checkbox" /><span>2 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<input type="checkbox" /><span>3 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<input type="checkbox" /><span>4 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<input type="checkbox" /><span>5 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<input type="checkbox" /><span>6 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<input type="checkbox" /><span>7 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<input type="checkbox" /><span>8 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<input type="checkbox" /><span>9 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<button>ORDER All Articles Checked !</button>
</p>
Cart-
<p id="ordered">
- - -
</p>

<script>
function id2ob(elmtID)
{
	return document.getElementById(elmtID);
}

id2ob("avail1").getElementsByTagName("button")[0].onclick = function() 
{
	//alert("Button Click");
	
	var cks = id2ob("avail1").getElementsByTagName("input"),
	sns = id2ob("avail1").getElementsByTagName("span"),
	cart=[],
	cost=0,
	out1='';
	
	for (var c=0;c<cks.length;++c) 
	{
		if(cks[c].checked) 
		{
			cart.push(sns[c].innerHTML.substr(0, 23));
		}
	}
	
	if(!cart.length)
	{
		alert("No Items Checked, NO ORDER"); 
		return
	}
	
	switch(cart.length) 
	{
	  case 9: cost=60; break;
	  case 8: cost=70; break;
	  case 7: cost=60; break;
	  case 6: cost=50; break;
	  case 5: cost=45; break;
	  case 4: cost=35; break;
	  case 3: cost=25; break;
	  case 2: cost=20; break;
	  case 1: cost=10; break;
	  default: cost=0;
	}
	  
	if(!cost)
	{
		alert("No Items Checked, NO ORDER"); 
		return
	}

	out1 += "<b>Your Total Cost for "+cart.length+" Articles is $"+cost+".00</b><br />Articles Ordered -<p>";

	for (c=0;c<cart.length;++c) 
	out1 +=cart[c]+"<br />";

	id2ob("ordered").innerHTML = out1;
	
}
</script></body></html>

Open in new window


What I need to do is take the selected articles and insert them into a hidden form field with each title separated by by a comma. And then the combined total, based on the number of articles has to be in yet another hidden field.

How?
Bruce GustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Flabio GatesCommented:
See this jsfiddle.
Basically, you add two hidden inputs:
  <input type="hidden" id="titles" />
  <input type="hidden" id="total" />

Open in new window

Then set their values via javascript/JQuery:
  $("#titles").val(cart.join(","));
  $("#total").val(cost);

Open in new window

Leonidas DosasCommented:
I added a form in your html code.And in the js when you click on the button then id adds a new hidden element in the form.
<!DOCTYPE html>
<html lang=en><head>
<title>Get Checks and ADD</title>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
<style>
body{background:#fed;}

#avail1 {
font-family: serif;
width: 48em;
padding: 5px;
border: 1px dotted #00b;
}

#avail1 input {
background: #99d;
margin-right:25px;
margin-bottom: 38px;
vertical-align: top;
}

#avail1 span {
display: inline-block;
width: 33em;
vertical-align: top;
}

#avail1 button {
margin: 7px 42px;
vertical-align: top;
}

#ordered {
margin-top:3px;
width: 48em;
padding: 5px;
border: 1px dotted #b00;
}
</style>
</head>
<body>
<h3>Get Checks and ADD</h3>
Order Online by Checking the Article below<br />
$10.00 per Article or $25.00 for Three, or $60.00 for all Nine Articles.
<p id="avail1">
<input type="checkbox" /><span>1 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<input type="checkbox" /><span>2 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<input type="checkbox" /><span>3 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<input type="checkbox" /><span>4 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<input type="checkbox" /><span>5 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<input type="checkbox" /><span>6 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<input type="checkbox" /><span>7 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<input type="checkbox" /><span>8 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<input type="checkbox" /><span>9 Developmental Article Fill More Fill More Fill More Fill More Fill More Fill More Fill More - Vol 5 Number 2, pages 3-7</span><br />
<button>ORDER All Articles Checked !</button>
</p>
Cart-
<p id="ordered">
- - -
</p>
<form action="" >
    
</form>
<script>
function id2ob(elmtID)
{
	return document.getElementById(elmtID);
}

id2ob("avail1").getElementsByTagName("button")[0].onclick = function() 
{
	//alert("Button Click");	
  
	var cks = id2ob("avail1").getElementsByTagName("input"),
	sns = id2ob("avail1").getElementsByTagName("span"),
	cart=[],
	cost=0,
	out1='';
	
	for (var c=0;c<cks.length;++c) 
	{
		if(cks[c].checked) 
		{
            var inputHidden=document.createElement('input');
            inputHidden.setAttribute("type", "hidden");
			cart.push(sns[c].innerHTML.substr(0, 23));
            inputHidden.value=sns[c].innerHTML.substr(0, 23);
            document.getElementsByTagName('form')[0].append(inputHidden);
		}
	}
	
	if(!cart.length)
	{
		alert("No Items Checked, NO ORDER"); 
		return
	}
	
	switch(cart.length) 
	{
	  case 9: cost=60; break;
	  case 8: cost=70; break;
	  case 7: cost=60; break;
	  case 6: cost=50; break;
	  case 5: cost=45; break;
	  case 4: cost=35; break;
	  case 3: cost=25; break;
	  case 2: cost=20; break;
	  case 1: cost=10; break;
	  default: cost=0;
	}
	  
	if(!cost)
	{
		alert("No Items Checked, NO ORDER"); 
		return
	}

	out1 += "<b>Your Total Cost for "+cart.length+" Articles is $"+cost+".00</b><br />Articles Ordered -<p>";

	for (c=0;c<cart.length;++c) 
	out1 +=cart[c]+"<br />";

	id2ob("ordered").innerHTML = out1;
	
}
</script></body></html>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bruce GustPHP DeveloperAuthor Commented:
Thank you, guys! Sorry for dragging me feet in getting back with you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.