Javascript populate textarea from checkbox

I'm trying to populate or clear a html textarea using a form checkbox.  I keep getting an unterminated string constant on the line probably becuase of the line breaks in the data.

Here is my js function

function updatefields() {
      if ($('grp_info_checkbox').checked == true) {
            alert('true');
            $('address').value = "<?=$_SESSION['group_info']['address'];?>";
      } else {
            alert('false');
            $('address').value = '';
      }
}

And my check box :

<input type="checkbox" name="grp_info_checkbox" value="1" checked="checked" onchange="updatefields()" />

Can somebody let me know how i could deal with such data?

Thanks

Chris
Sailo100Asked:
Who is Participating?
 
Michel PlungjanIT ExpertCommented:

function updatefields(theChk) {
  theChk.form.address.value = (theChk.checked) ?
    unescape("<?= urlencode($_SESSION['group_info']['address']) ?>".replace(/\+/g,' ')):"";
}
0
 
yaxhCommented:
You were mixing JavaScript with PHP. See below if it works

if (document.getElementByID('grp_info_checkbox').checked) {
            alert('true');
         document.getElementByID(('address').value = "<?=$_SESSION['group_info']['address'];?>";
      } else {
            alert('false');
            document.getElementByID('address').value = '';
      }
}

<input type="checkbox" id="grp_info_checkbox" value="1" checked="checked" onchange="updatefields()" />
0
 
yaxhCommented:
Sorry for spam but function name if 'getElementById' not 'getElementByID'
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Michel PlungjanIT ExpertCommented:
No he is not.
He is using prototype.js and the shorthand for document.getElementById() is $()


Here

function updatefields(theChk) {
  theChk.form.address.value = (theChk.checked) ?
    unescape("<?= urlencode($_SESSION['group_info']['address'];) ?>"):"";
}

And your check box :

<input type="checkbox" name="grp_info_checkbox" value="1" checked="checked" onchange="updatefields(this)" />
0
 
Sailo100Author Commented:
I am only mixing php with javascript in regards to the session variables.

This - $('grp_info_checkbox') is a simple method of referencing an element using the prototype javascript framework.

Anyway.... i get the same results with your function!
0
 
Michel PlungjanIT ExpertCommented:
If not in the same form and still using prototype:



function updatefields(theChk) {
  $('address').value= (theChk.checked) ?
    unescape("<?= urlencode($_SESSION['group_info']['address']) ?>"):"";
}

and you should use

onClick="updatefields(this)"

instead of onChange

ps: I had a trailing semicolon inside the urlencode
0
 
Sailo100Author Commented:
with this i get $('address') has no properties :

function updatefields(theChk) {
 $('address').value = (theChk.checked) ?
    unescape("<?= urlencode($_SESSION['group_info']['address']) ?>"):"";
}


with this it works but then i have + where spaces should be?

function updatefields(theChk) {
  theChk.form.address.value = (theChk.checked) ?
    unescape("<?= urlencode($_SESSION['group_info']['address']) ?>"):"";
}

0
 
Michel PlungjanIT ExpertCommented:
PS:
If you with my code get $('address') has no properties, then you would ALSO have gotten it with

function updatefields() {
      if ($('grp_info_checkbox').checked == true) {
            alert('true');
            $('address').value = "bla bla bla";
      } else {
            alert('false');
            $('address').value = '';
      }
}
0
 
Michel PlungjanIT ExpertCommented:
Which leads me to believe your code is

<textarea name="address">

and not

<textarea id="address" name="address">

as it must be to use getElementById
0
 
Sailo100Author Commented:
Yes you are quite right i had no id set on the textarea!

Thank you for your helpful information mplunigian!

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.