tags:

views:

25

answers:

3

hey guys

this is my raw javascript code :

<script language="javascript" type="text/javascript" > 
  function S2T(e) {
  document.comments.comment.value = document.comments.comment.value + e;
  } 
</SCRIPT>

and i want to convert it to jquery function

<script language="javascript" type="text/javascript" > 
$(document).ready(function() {
  function S2T(e) {
  $("textarea#comment").val($('#comment').val()+e); 
 });
</SCRIPT>

html part

<a href='javascript:S2T('please')' >

i cant convert it , i just want to write function when i click on a icon , it adds to textarea

+1  A: 

Writing function definition in to $(document).ready() binds it to the ready event of the document. This means the code will execute once the page has finished loading. This isn't what you wanted.

Since you have jQuery loaded, you can simply use it within your already existing function, such as:

<script  language="javascript" type="text/javascript">
function S2T(e) {
  $("textarea#comment").val($('#comment').val()+e);
}
</script>

And use it with your existing html:

<a href='javascript:S2T("please")' >
nnevala
A: 
<script language="javascript" type="text/javascript" > 
  function S2T(e) {
    $("textarea#comment").val($('#comment').val()+e); 
  }
</SCRIPT>

The document ready is not necesary here. Also you had a quote missing

Diego
+1  A: 

You can define functions just as you used to, no need to put it in the ready() call:

<script language="javascript" type="text/javascript" > 
  function S2T(e) {
     $("textarea#comment").val($('#comment').val()+e); 
  }
</script>

then make sure you don't use single-quoted javascript strings inside single-quoted HTML attributes:

<a href='javascript:S2T("please")' >
Ned Batchelder