Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Javascript populate textarea from checkbox

Posted on 2007-08-09
10
Medium Priority
?
264 Views
Last Modified: 2013-11-05
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
0
Comment
Question by:Sailo100
  • 5
  • 3
  • 2
10 Comments
 
LVL 2

Expert Comment

by:yaxh
ID: 19661299
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
 
LVL 2

Expert Comment

by:yaxh
ID: 19661308
Sorry for spam but function name if 'getElementById' not 'getElementByID'
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 19661386
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Sailo100
ID: 19661391
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 19661403
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
 

Author Comment

by:Sailo100
ID: 19661495
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
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 total points
ID: 19661760

function updatefields(theChk) {
  theChk.form.address.value = (theChk.checked) ?
    unescape("<?= urlencode($_SESSION['group_info']['address']) ?>".replace(/\+/g,' ')):"";
}
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 19661777
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 19661858
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
 

Author Comment

by:Sailo100
ID: 19662392
Yes you are quite right i had no id set on the textarea!

Thank you for your helpful information mplunigian!

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question