Mac Taylor
Mac Taylor

Reputation: 5166

problem in writing a function in jquery

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

Upvotes: -1

Views: 78

Answers (3)

Diego
Diego

Reputation: 16714

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

The document ready function is not necessary here. Also, you had a quote missing in the code.

Upvotes: 0

Ned Batchelder
Ned Batchelder

Reputation: 375992

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")' >

Upvotes: 1

nnevala
nnevala

Reputation: 5997

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")' >

Upvotes: 1

Related Questions