VelvetMirror Posted December 7, 2010 Share Posted December 7, 2010 So I would like to know if there is a better standar practice to ask for multiple values instead of using prompts (which I think are very annoying), without knowing beforehand how many values be need. Simple example program with prompts (is there a better way to do this?): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Sum a list of numbers</title> <script type="text/javascript"> function sum() { var num=1; var sum=0; while(num!=0 && num!=null ) { num=prompt("Insert a number (press 0 to exit)"); if(num!=null && !isNaN(parseInt(num))) { num=parseInt(num); sum=sum+num; } } if(num==0) { alert("Sum: "+sum); } } </script> </head> <body onload="sum();"> </body></html> Link to comment Share on other sites More sharing options...
jeffman Posted December 7, 2010 Share Posted December 7, 2010 I would suggest having a text input in the body of your document, and a button next to it labeled "Sum" or something like that. When the user hits the sum button, get the data from the input and execute your function. Put the resulting sum into the innerHTML property of a nearby <p> element, or something similar. Link to comment Share on other sites More sharing options...
ShadowMage Posted December 7, 2010 Share Posted December 7, 2010 Or a series of text inputs, and when the user fills one in a new one appears below it. This can be easily done with JavaScript using document.createElement. You could give them id's like 'input1', 'input2', etc. so that they are easily looped through in JavaScript. Then clicking the sum button would do the looping and adding, then either set the innerHTML of another element or show an alert. Link to comment Share on other sites More sharing options...
thescientist Posted December 7, 2010 Share Posted December 7, 2010 Or a series of text inputs, and when the user fills one in a new one appears below it. This can be easily done with JavaScript using document.createElement. You could give them id's like 'input1', 'input2', etc. so that they are easily looped through in JavaScript. Then clicking the sum button would do the looping and adding, then either set the innerHTML of another element or show an alert.fancy pantsy :) Link to comment Share on other sites More sharing options...
VelvetMirror Posted December 7, 2010 Author Share Posted December 7, 2010 Or a series of text inputs, and when the user fills one in a new one appears below it. This can be easily done with JavaScript using document.createElement. You could give them id's like 'input1', 'input2', etc. so that they are easily looped through in JavaScript. Then clicking the sum button would do the looping and adding, then either set the innerHTML of another element or show an alert.Do you mean to insert a new input using the "onfocus" event?I thought about that. But what if for example if I only want to insert two numbers, when I type on the second input box, a third one would appear automatically which I don't need. But may be it's the best option anyway, I will try to do that. Link to comment Share on other sites More sharing options...
jeffman Posted December 8, 2010 Share Posted December 8, 2010 A more friendly interface would have an actual button to add a new input.FWIW, I know nothing about you, so I can't really say anything about your skill level. I'm just observing that your first post showed some very elementary JavaScript. Now you seem to be going along with the idea of adding elements to the DOM, which is usually considered a bit advanced. Are you up to this?Again, apologies if I'm misreading the situation. You do seem to understand events, which the first post would not have led me to expect either. Link to comment Share on other sites More sharing options...
thescientist Posted December 8, 2010 Share Posted December 8, 2010 Do you mean to insert a new input using the "onfocus" event?I thought about that. But what if for example if I only want to insert two numbers, when I type on the second input box, a third one would appear automatically which I don't need. But may be it's the best option anyway, I will try to do that.you would be testing for values in the inputs anyway, right? So by validation standards, if the input value was equal to an empty string, or null, or letters, or something unwanted, you would not use it anyway. This would go for the first, the second, etc inputs, all the way up to the end. Link to comment Share on other sites More sharing options...
ShadowMage Posted December 8, 2010 Share Posted December 8, 2010 A more friendly interface would have an actual button to add a new input.I beg to differ. I prefer an interface that does it for me.Like scientist said, it's not really an issue if you only need two numbers, but a third input appears since you'd be testing the values of the inputs to make sure they are usable anyway. Or at least you should be. Link to comment Share on other sites More sharing options...
jeffman Posted December 8, 2010 Share Posted December 8, 2010 It depends I suppose on factors only OP knows. What I meant was, if I think I'm done, I don't want an empty input appearing for me, suggesting I'm wrong when I think I'm done. Link to comment Share on other sites More sharing options...
thescientist Posted December 8, 2010 Share Posted December 8, 2010 I beg to differ. I prefer an interface that does it for me.I wouldn't mess with Spartacus... Link to comment Share on other sites More sharing options...
jeffman Posted December 8, 2010 Share Posted December 8, 2010 . . . unless you have waist-length hair, fishnet stockings, and heels to die for. Link to comment Share on other sites More sharing options...
thescientist Posted December 8, 2010 Share Posted December 8, 2010 . . . unless you have waist-length hair, fishnet stockings, and heels to die for. naturally Link to comment Share on other sites More sharing options...
VelvetMirror Posted December 8, 2010 Author Share Posted December 8, 2010 Thanks all for your inputs, you have pointed me in the right direction and give me ideas how to fix this, I will post the final code when it's ready. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.