Need some help with adding text to text area onsubmit

Discussion in 'JavaScript' started by TDave00, Sep 23, 2009.

  1. #1
    I am trying to add some predefined text (actually html) to a text area on submit. It's important that the predefined text precede the existing text when combined.

    I don't know a lot about javascript. I just piece stuff together. After some research here is some code I put together.

    function changeText() {
            var addtext = "<img src="" />";
    	var extext = document.forms[0].getElementsByTagName("textarea")[0].value;
    	var newtext = addtext + extext;
    	document.forms[0].getElementsByTagName("textarea")[0].value = newtext;
    }
    Code (markup):
    This "should" :

    1) Set the predefined text. (addtext)
    2) Get the existing text in the text area. (extext)
    3) Combine the predefined text and the existing text. (newtext)
    4) Change the textarea value to the new combined text.

    How would I call that and then get the form to submit? Thanks.
     
    Last edited: Sep 23, 2009
    TDave00, Sep 23, 2009 IP
  2. prasanthmj

    prasanthmj Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    45
    #2
    update the form tag this way:
    
    <form onsubmit='changeText(); return true;'   ...>
    
    Code (markup):
     
    prasanthmj, Sep 23, 2009 IP
  3. TDave00

    TDave00 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I am working on an addon for firefox, so all this is to modify a form on a page that's not mine.

    The form on the page doesn't have onsubmit defined, so how would I inject that with javascript?

    document.forms[0].onsubmit="changeText(); return true;";
    Code (markup):
    ??
     
    TDave00, Sep 23, 2009 IP
  4. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    try:
    
    document.forms[0].onsubmit=function() {
                 var addtext = "<img src='' />";
    	var extext = document.forms[0].getElementsByTagName("textarea")[0].value;
    	var newtext = addtext + extext;
    	document.forms[0].getElementsByTagName("textarea")[0].value = newtext;
                 return true;
    }
    
    Code (markup):
     
    camjohnson95, Sep 27, 2009 IP