Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can this be done in a loop? 3

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
Is it possible to do loops to replace this?

<option value="0">0</option>
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
<option value="50">50</option>
<option value="60">60</option>

Having to pre-enter 10-500+, seems so long winded doing several combo boxes. Thanks
 
Not in HTML. HTML is a markup language, not a scripting/programming language.

But you can using Javascript or a server side language of choice (my preferred option).

Foamcow Heavy Industries - Web design and ranting
Target Marketing Communications - Advertising, Direct Marketing and Public Relations
I wonder what possesses people to make those animated gifs. Do you just get up in the morning and think, "You know what web design r
 
Many thanks, fast reply!! I will have to hard code it all in then as no server side available, and if someone turns off java then it presumably wont work. Thank.
 
Having to pre-enter 10-500+, seems so long winded doing several combo boxes.
Indeed! But why do it that way? Why not just let them type in a number?! By adding so many menu options, some browsers will choke... just attempting to click on the select using some of the systems I test with would most likely freeze them for up to 10 seconds for such menus.

Just a thought.

Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Here you go. Hopefully you'll get it before starting on the 'type-athon' [bigsmile]

Code:
<html>
<head>
	<script type="text/javascript">
	<!--
		function createSelect(selName, startValue, endValue, valueIncrement) {
			var htmlStr = '<select name="' + selName + '">\n'
			for (var loop=startValue; loop<=endValue; loop+=valueIncrement) {
				htmlStr += '\t<option value="' + loop + '">' + loop + '</option>\n';
			}
			htmlStr += '</select>\n';
			document.getElementById('codeBox').value = htmlStr;
		}
	//-->
	</script>
</head>
<body onload="createSelect('aSelect', 0, 500, 10);">
	<textarea id="codeBox" style="width:600px; height:400px;"></textarea>
</body>
</html>

Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Indeed, I would just use an input text box. Much easier for me AND for the user.

If the value has to be a multiple of 10, put a note next to the box and use Javascript/server side script to validate the input.

Foamcow Heavy Industries - Web design and ranting
Target Marketing Communications - Advertising, Direct Marketing and Public Relations
I wonder what possesses people to make those animated gifs. Do you just get up in the morning and think, "You know what web design r
 
Blimey, thanks for all the replies. The people seeing the page are definately prone to making errors, unless they see a number they are more likely to enter an invalid input. If I were to validate for 10,20,30,40,50-500+ etc am I going to end up with reams of code to prevent anything between? Would validation be written around individual inputs or could I do a global function? I presently have a function that checks for at least 1 input (combo selection) is greater than 0 in order for sendmail to be used. Is their a way this could be modified and a messagebox appear saying "Idiot, wrong numeric choice" or whatever. Code used below. Many, Many thanks.

function chkForm() {

var DB6 = document.getElementById('DB6').value;
var DB12 = document.getElementById('DB12').value
var DB22 = document.getElementById('DB22').value
var DB32 = document.getElementById('DB32').value
var DB34 = document.getElementById('DB34').value
var DB40 = document.getElementById('DB40').value
var DB64 = document.getElementById('DB64').value
var DB94 = document.getElementById('DB94').value
var DB124 = document.getElementById('DB124').value

var SP05 = document.getElementById('SP05').value;
var SP10 = document.getElementById('SP10').value
var SP20 = document.getElementById('SP20').value
var SP30 = document.getElementById('SP30').value
var SP60 = document.getElementById('SP60').value
var SP90 = document.getElementById('SP90').value

var HD6 = document.getElementById('HD6').value;
var HD22 = document.getElementById('HD22').value;
var HD40 = document.getElementById('HD40').value;
var HD34 = document.getElementById('HD34').value;
var HD94 = document.getElementById('HD94').value;

var DVC12 = document.getElementById('DVC12').value;
var DVC33 = document.getElementById('DVC33').value;
var DVC46 = document.getElementById('DVC46').value;
var DVC66 = document.getElementById('DVC66').value;
var DVC126 = document.getElementById('DVC126').value;


if(DB6=='0' && DB12=='0' && DB22=='0' && DB32=='0' && DB40=='0' && DB64=='0' && DB94=='0'
&& DB124=='0' && SP05=='0' && SP10=='0' && SP20=='0' && SP30=='0' && SP60=='0' && SP90=='0'
&& HD6=='0' && HD12=='0' && HD22=='0' && HD40=='0' && HD34=='0' && HD94=='0' && DVC12=='0'
&& DVC33=='0' && DVC46=='0' && DVC66=='0' && DVC125=='0') {


alert('You have not selected any items?');
return false;
} else {
update_message_body();
}
}
</script>
<base target="_self">


 
Since you seem fine with using Javascript... here's a solution to your iitial problem! Add this to the top of the page (somewhere in the head section maybe):
Code:
<script type="text/javascript">
function makeOptions(_number_of_options) {
  var _result = '';
  for (var loop=0; loop<_number_of_options; loop++)
    _result += '\t<option value="' +loop*10 + '">"' +loop*10 + '"</option>\n';
  return _result;
}
</script>
You would use this code as follows within your HTML -- note the line of javascript where you can specify how many options you want to include:
Code:
...
<div>
Choose from the  list: <select name="DB124">
[COLOR=red]<script type="text/javascript">document.write(makeOptions([COLOR=blue]7[/color]));</script>[/color]
</select>
</div>
...
<div>
Choose from the  list: <select name="DB125">
[COLOR=red]<script type="text/javascript">document.write(makeOptions([COLOR=blue]3[/color]));</script>[/color]
</select>
</div>
...
This will generate the following code (as rendered by the browser):
Code:
<div>
...
<div>
Choose from the  list: <select name="DB124">
  <option value="10">10</option>
  <option value="20">20</option>
  <option value="30">30</option>
  <option value="40">40</option>
  <option value="50">50</option>
  <option value="60">60</option>
  <option value="70">70</option>
</select>
</div>
...
<div>
Choose from the  list: <select name="DB125">
  <option value="10">10</option>
  <option value="20">20</option>
  <option value="30">30</option>
</select>
</div>
...
OK... that is one solution to the problem. There are others that are going to be a LOT more accessible and that degrade better without javascript etc. This code will hopefully be all you need to get started.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
ZOR,

Your latest post shows that you're using JavaScript, yet your second post implies that you did not want a JavaScript solution.

Can you clarify whether you are, or whether you are not, after a JavaScript solution?

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Wonderful BabyJeffy, works a treat. Have a well deserved star.
The code I am using is a modified page that someone provided. I have no idea whether it's script, Javascript, Greek. Unfortunately it must have complicated things showing parts HTML and parts whatever. BillyRayPreacherSon you get a star as well, clever coding again. A star to Foamcow, adding to the pull away from a heavy page. Best regards All Thanks again.
 
Just a quickie, is there a way to eliminate the ** each side of the numberical count selection in the combo? Thanks
 
Thanks, the combo values appear as *0" or *30*. They don't get there on the email.
 
to prevent having to validate for "10,20,30,40....500+", just use a text box then see if the input is divisible by 10 with no remainder.

My javascript is rustier than Micheal Jackson's bank account, but you'd be looking for a modulous function.

Foamcow Heavy Industries - Web design and ranting
Target Marketing Communications - Advertising, Direct Marketing and Public Relations
I wonder what possesses people to make those animated gifs. Do you just get up in the morning and think, "You know what web design r
 
Foamcow said:
My javascript is rustier than Micheal Jackson's bank account...
[rofl]
Bad boy!

Zor, the *XX* things you are talking about are probably a case of bad copy-paste along the way. Probably the easiest thing at this point would be for you to post your javascript code (that is relevant to where the <option> is built up).

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Thanks Jeff.

THE FUNCTION
<script type="text/javascript">
function makeOptions(_number_of_options) {
var _result = '';
for (var loop=0; loop<_number_of_options; loop++)
_result += '\t<option value="' +loop*10 + '">"' +loop*10 + '"</option>\n';
return _result;
}
</script>



THE INDIVIDUAL BITS
<tr>
<td class="DescriptorCell" width="19%" bgcolor="#FFFFFF">DBC-12</td>
<td class="DescriptorCell" bgcolor="#FFFFFF" width="34%">12mins</td>
<td class="ContentCell" width="9%" bgcolor="#FFFFFF">
<select name="DB12">
<script type="text/javascript">document.write(makeOptions(100));</script>
</select>
</td>
</tr>

 
I have sorted it Jeff. I removed the " from the line below. All seems to work well.

_result += '\t<option value=' +loop*10 + '>' +loop*10 + '</option>\n';
 
Put your loop * 10 inside parentheses:

'<option value=' +(loop*10) + '>' +(loop*10) + '</option>\n'

Since you're creating this as a string, the loop * 10 is evaluated as the value in loop plus '*', I'd guess.

Lee
 
Thanks trollacious, the code I ended up with seems to be fine. I will try your suggestion if it starts packing up. Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top