PDA

View Full Version : Sentence Generator



julmado
05-14-2006, 04:46 AM
Greetings!

I am looking for a very simple script that does not seem to exist..!

It would be a series of drop-down boxes with text in them, and, after selecting a combination, a button which would gather all the text select by drop-downs and paste them into a field.

I'll try to examplify:

~ is the next drop down box


I ~ like ~ Dynamic Drive
You ~ likes ~ DHTML
He

The sumbit button would paste either:

I like Dynamic Drive
You like Dynamic Drive
He likes Dynamic Drive

Into a text area, depending on what was selected.

Has anyone seen this kind of script around?

I will, of course combine this with the chained selects script and possibly a radio button to predefine what type of "sentence" would be loaded into the drop-downs.

Twey
05-14-2006, 08:03 AM
<form action="" onsubmit="
for(var i=0;this.elements['s' + i + '-1'];i++) {
for(var j=0;this.elements['s' + i + '-' + j];i++) {
this.elements['paragraph'].value += (j == 0 ? "" : " ") + this.elements['s' + i + '-' + j].value;
}
this.elements['paragraph'].value += '.\n';
}
">
<select name="s1-1">
<option>I</option>
<option>You</option>
<option>He</option>
</select>
<select name="s1-2">
<option>like</option>
<option>likes</option>
</select>
<select name="s1-3">
<option>Dynamic Drive</option>
<option>DHTML</option>
<option>cats</option>
</select>
<input type="submit">
<textarea name="paragraph"></textarea>
</form>Untested.

julmado
05-19-2006, 04:17 AM
You're on the right path, but it doesn't seem to work..!

Still scouring the web..

Twey
05-19-2006, 10:51 AM
Here we go... this one works.
<form action="" onsubmit="
for(var i=1;this.elements['s' + i + '-1'];i++) {
for(var j=1;this.elements['s' + i + '-' + j];j++) {
this.elements['paragraph'].value += (j == 0 ? '' : ' ') + this.elements['s' + i + '-' + j].value;
}
this.elements['paragraph'].value += '.\n';
}
return false;
">
<select name="s1-1">
<option>I</option>
<option>You</option>
<option>He</option>
</select>
<select name="s1-2">
<option>like</option>
<option>likes</option>
</select>
<select name="s1-3">
<option>Dynamic Drive</option>
<option>DHTML</option>
<option>cats</option>
</select>
<input type="submit">
<textarea name="paragraph"></textarea>
</form>

julmado
05-21-2006, 04:11 PM
This.. doesn't work in IE but is fine in Opera.

Twey
05-21-2006, 04:39 PM
Ah -- try:
<form action="" onsubmit="
for(var i=1;this.elements['s' + i + '-1'];i++) {
for(var j=1;this.elements['s' + i + '-' + j];j++) {
this.elements['paragraph'].value += (j == 1 ? '' : ' ') + (this.elements['s' + i + '-' + j].value || this.elements['s' + i + '-' + j].options[this.elements['s' + i + '-' + j].selectedIndex].innerHTML);
}
this.elements['paragraph'].value += '.\n';
}
return false;
">
<select name="s1-1">
<option value="">I</option>
<option>You</option>
<option>He</option>
</select>
<select name="s1-2">
<option>like</option>
<option>likes</option>
</select>
<select name="s1-3">
<option>Dynamic Drive</option>
<option>DHTML</option>
<option>cats</option>
</select>
<input type="submit">
<textarea name="paragraph"></textarea>
</form>

julmado
05-21-2006, 05:59 PM
Hah, what an insult to IE. Every time I dip into HTML/JS I sympathise with those that have to deal with this ridiculousness every day.
Many thanks Twey, I will show you what I end up with after a bit of tweaking around..

Twey
05-21-2006, 06:21 PM
Hah, what an insult to IE. Every time I dip into HTML/JS I sympathise with those that have to deal with this ridiculousness every day.Yeah, it is a bit daft writing a script for everything else, then having to do half of it again just for IE. I don't know if this is a case of that, though; I was relying on the browser, if finding no explicitly declared value for the option, using the option's text. Opera and Firefox, apparently, do this; IE doesn't. Whether it's correct behaviour or not, I don't know.

julmado
07-08-2006, 03:59 AM
I am looking to combine the above script this the chained selects script:
http://www.dynamicdrive.com/dynamicindex16/chainedselects/index.htm

Is this possible?

I am guessing not because all the values for the chained selects are in a .js file.

Twey
07-08-2006, 04:09 AM
No, that should be no problem. Just keep the onsubmit the way it is, and so long as the Chained Selects doesn't require you to modify the names of the elements, there's no reason why not.

julmado
07-08-2006, 04:26 AM
I am looking at the chainedselects.js and there are so many "for (var" entries I would not know where to start.

I can grasp your code, Twey:


<form action="" onsubmit="
for(var i=1;this.elements['s' + i + '-1'];i++) {
for(var j=1;this.elements['s' + i + '-' + j];j++) {
this.elements['paragraph'].value += (j == 1 ? '' : ' ') + (this.elements['s' + i + '-' + j].value || this.elements['s' + i + '-' + j].options[this.elements['s' + i + '-' + j].selectedIndex].innerHTML);
}
this.elements['paragraph'].value += '.\n';
}
return false;
">

...but the hefty .js file is way beyond me.

Time to put on the coffee.

Twey
07-08-2006, 04:39 AM
I don't think you need to mess with the .js file, after a cursory glance at the instructions.

julmado
07-12-2006, 12:30 PM
I'm at a loss..

Would I just have to replace


<body onload="initListGroup('vehicles', document.forms[0].make, document.forms[0].type, document.forms[0].model, 'cs')">

By


<body onload="initListGroup('vehicles', document.forms[0].s1-1, document.forms[0].s1-2, document.forms[0].s1-3, 'cs')">

Twey
07-12-2006, 12:44 PM
Essentially, yes, but you should use the elements collection (for example,
document.forms[0].elements['s1-1']).