Jump to content

Does javascript support .= ?


TeddyKiller

Recommended Posts

I have this little script. Though it doesn't do anything.. it doesn't populate the div. Any help?

<script>
function selectMenu(VALUE) {
DIV.document.getElementById('selects');

for(VAL = 0; VAL <= VALUE; VAL++) {
	HTML = HTML + 'Choice ' + VAL + '<input type="text" name="field"' + VAL + '" /><br />';
}

DIV.innerHTML = HTML;
}
</script>
<?php
echo '<form action="" method="post">
	Poll Name<input type="text" name="pollName" /> <br />
	<select name="fields" onchange="selectMenu(this.value)">
		<option value=""> </option>';
	for($i = 1; $i < 11; $i++) {
		echo '<option value="' . $i . '">' . $i . '</option>';
	}
	echo '</select>
	<div id="selects"></div>
	<input type="submit" name="submit" value="create" />';
?>

Link to comment
Share on other sites

You would use += because unlike PHP where to concatenate strings you use . in JavaScript you use +.

 

function selectMenu(VALUE) {
        var HTML = '';
for(var VAL = 0; VAL <= VALUE; VAL++) {
	HTML =+ 'Choice ' + VAL + '<input type="text" name="field"' + VAL + '" /><br />';
}

document.getElementById('selects').innerHTML = HTML;
}

Link to comment
Share on other sites

Whoops, I had a typo. You also had a problem with your HTML.

 

function selectMenu(VALUE) {
        var HTML = '';
for(var VAL = 0; VAL <= VALUE; VAL++) {
	HTML += 'Choice ' + VAL + '<input type="text" name="field' + VAL + '" /><br />';
}

document.getElementById('selects').innerHTML = HTML;
}

Link to comment
Share on other sites

Great thanks! Now.. theres a problem. If you type in some of the textboxes, change the .. drop down value.. it'll reload the inputs. I would like it so if .. someone filled in 5.. and 8 were selected.. then the user switched the drop down to 5.. it'll take off the bottom 3. Equalling 5.. without changing the values inside the 5 boxes filled. .. if you get me.

 

<script>
function selectMenu(VALUE) {
var HTML = '';

HTML += '<table>';
for(var VAL = 1; VAL <= VALUE; VAL++) {
	HTML += '<tr><td>Choice ' + VAL + '</td><td><input type="text" name="field' + VAL + '" /></td></tr>';
}
HTML += '</table>';

document.getElementById('selects').innerHTML = HTML;
}
</script>
<style>
table.td {
padding:5px;
}
</style>
<?php

echo '<form action="" method="post">
<table>
	<tr><td>Poll Name</td><td><input type="text" name="pollName" /></td></tr>
	<tr><td>Number of fields</td><td><select name="fields" onchange="selectMenu(this.value)">
		<option value="" selected disabled> </option>';
	for($i = 1; $i < 11; $i++) {
		echo '<option value="' . $i . '" '; if($i == 1) { echo 'disabled'; } echo '>' . $i . '</option>';
	}
	echo '</select></td></tr>
	</table>
	<div id="selects"></div>
	<table><tr><td><input type="submit" name="submit" value="create" /></td><td></td></tr></table>
</form>';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.