Inserting value into textarea?

Hi guys,

Are these codes correct below?

            $("#done").click(function(){
                $("div #right-answer").show();
                var string_ans = '<?php  echo $ans ?>';
                $("#right-answer").val(string_ans);                 
            });    

The idea is when the button #done is clicked it will insert the value from var string_ans into
texxtarea #right-answer.

I tried the codes but it did not work.

Please help me correct the codes.

Thans in advance.

Here is the updated version.

   <script>
        var string_ans = '<?php  echo $ans ?>;
       
        $(function () {
            $("div #right-answer").hide();
            $("div #reference").hide();
            
            $("#done").click(function(){
                $("div #right-answer").val(string_ans.val());                 
                $("div #right-answer").show();
                $("div #reference").show();     
                
            });
        });    
    </script>

The problem is, it is not working.
What might be the reason?

Any one would like to help me?

Thankks in advance.

Okay since I’m inserting the value into textarea, Here is the updated codes.

   <script>
        var string_ans = '<?php  echo $ans ?>;
       
        $(function () {
            $("div #right-answer").hide();
            $("div #reference").hide();
            
            $("#done").click(function(){
                $("textarea #right-answer2").val(string_ans.val());                 
                $("div #right-answer").show();
                $("div #reference").show();     
                
            });
        });    
    </script>

But still not working.
What’s wrong with my codes?

Thanks in advance.

The most immediate problem is that the string_ans line is missing a closing quote symbol.

Another part that might be problematic is this one

$("textarea #right-answer2").val(string_ans.val());

as anything within a textarea will be treated as plain text. Do you maybe mean

$("textarea#right-answer2").val(string_ans.val());

i.e. the textarea with the ID right-answer2? Note though that the textarea selector would be redundant in this case as an ID is supposed to be unique anyway.

And then, if string_ans is supposed to be a string, remove the .val() bit as this only returns the value of input elements. If it’s a string, this would yield a type error. Always listen to what your console says! ;-)

That might be a technique that they are not aware of yet.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.