Jump to content

checkbox onclick help


MDanz

Recommended Posts

i want if this checkbox is clicked to change the value in the textarea to "Input image url".. the below isn't working though

 

<input type='checkbox' name='image' onclick='if(this.checked) { image(0);}' />

 

function image(number) {

document.outcomes['reason["+number+"]'].value="Input image url";


}

 

<form action='submit.php' method='POST' name='outcomes'>
<textarea name='reason[0]'></textarea>
</form>

Link to comment
https://forums.phpfreaks.com/topic/242308-checkbox-onclick-help/
Share on other sites

'reason["+number+"]'

 

To concatenate the variable, you need to close/open the string using a single quote like you originally use to open/close it. Otherwise what you'll end up here with is literally: reason["+number+"].

 

Also you'd be better off changing the name of the function. "Image" is a default object, and although JS is case-sensitive, it doesn't seem to like you referencing it within the document. You could fix the problem by calling "window.image(0)" - but it would be better to change it to avoid such issues.

Please make this code as reference, I hope it helps you:

 

<script type="text/javascript">

function ChangeLabel() {

if (document.getElementById("image").checked == true) {

document.getElementById("textarea").innerHTML = 'Input Image URL';

}else{

document.getElementById("textarea").innerHTML = '';

}

}

</script>

<form>

<input type='checkbox' name='image' id="image" onclick="ChangeLabel();" />

<textarea id="textarea"></textarea>

</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.