0

I am trying to code a form that creates a custom sentence based on the inputs a user selects. For example, my form has one written inputs and two dropdowns. I want this data added to my innerHTML. Say I wanted to create a custom sentence about a dog the layout would look like this:

My dog's name is (value from input 1). (Value from dropdown 1) loves to play fetch. My dog is (value from dropdown 2).

function sentence() {
  document.getElementById("s1").innerHTML = "My dog's name is (value from input 1). (Value from dropdown 1) loves to play fetch. My dog is (value from dropdown 2).";
}
table,
td,
th {
  margin-left: auto;
  margin-right: auto
}

.display {
  display: flex;
  align-items: center;
  justify-content: center;
}

p {
  text-align: center;
}
<table>
  <tr>
    <td> <input type="text" placeholder="Your Dog's Name" name="name" maxlength="100"></td>
    <td>
      <select name="gender">
        <option value="He">He</option>
        <option value="She">She</option>
      </select>
    </td>
    <td>
      <select name="size">
        <option value="Big">Big</option>
        <option value="Medium">Medium</option>
        <option value="Small">Small</option>
      </select>
    </td>
</table>
<br>
<hr>
<br>
<p id="s1">

</p>
<br>

<div class="display">
  <button onclick="sentence()"> Button </button>
</div>

<p id="s1">

</p>

1
  • 1
    You can get the relevant data using document.getElementById('theid').value. All you need to do is change name to id (name is for forms that get sent to the server). Commented Jun 29, 2018 at 22:28

2 Answers 2

2

The following code will do the trick!

function sentence()
{
  const v1 = document.getElementsByTagName('input')[0].value,
        v2 = document.getElementsByTagName('select')[0].value,
        v3 = document.getElementsByTagName('select')[1].value
  document.getElementById("s1").innerHTML="My dog's name is " + v1 + ". " + v2 + "loves to play fetch. My dog is " + v3 + ".";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Beautiful. Thanks a million.
@PhilMotto if it's the answer vote it up or select it as the answer <3 (that's how this site works.) and welcome to the community!
1

Try this code :)

function sentence() {
	var nameDog = document.getElementById( "name" ),
		
		genderDog = document.getElementById( "gender" ),
		
		sizeDog = document.getElementById( "size" ),
		
		result = document.getElementById( "s1" );
	
	if ( nameDog.value == "" ) {
		
		result.innerHTML = "Write name your dog's";
		
	} else {
		
		result.innerHTML = "My dog's name is " + nameDog.value + ", " + genderDog.value + " loves to play fetch. My dog is " + sizeDog.value;
		
	}
};
table, td, th {
	margin-left: auto;
	margin-right: auto
}

.display {
	display: flex;
	align-items: center;
	justify-content: center;
}

p {
	text-align: center;
}
<table>
	<tr>
		<td><input id="name" placeholder="Your Dog's Name" type="text">
		</td>

		<td><select id="gender">
			<option value="he">
				he
			</option>

			<option value="she">
				she
			</option>
		</select>
		</td>

		<td><select id="size">
			<option value="big">
				big
			</option>

			<option value="medium">
				medium
			</option>

			<option value="small">
				small
			</option>
		</select>
		</td>
	</tr>
</table>
<br>

<hr>
<br>


<div class="display">
	<button onclick="sentence()">Button</button>
</div>


<p id="s1">
</p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.