www.webdeveloper.com
Results 1 to 5 of 5

Thread: Use value of textarea when button is clicked

  1. #1
    Join Date
    Apr 2012
    Posts
    6

    Use value of textarea when button is clicked

    My Code looks like (simplified) this:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    	<title>title</title>	
    	<script type="text/javascript" src="file.js"> </script> 
    </head>
    
    <body>
    	<textarea rows="10" cols="30" id="taIn"> </textarea>
    	<input type="button" id="btOne" onclick="" value="Press">
    	</input>	
            <table id="tabID"> ... </table>
    </body>
    
    </html>
    I want the user to be able to insert a text into "taIn". When he presses "btOne", a function "fnc(tableID, text)" (in file.js)processing the inserted text should be called and should edit content on the page, especially the table following it. How do I modify the attribute "onclick"?

    I tried
    Code:
    onclick={"fnc(tabID, " + taIn.value + ")"}
    though this does not seem to work. Any suggestions?

    Thank you in advance!

  2. #2
    Join Date
    Nov 2010
    Posts
    1,084
    there is almost certainly a better way to do this, but did you try:
    Code:
    <input type="button" id="btOne" onclick="fnc('tabID',document.getElementById('taIn').value)" value="Press">
    	</input>

  3. #3
    Join Date
    Apr 2012
    Posts
    6

    Lightbulb Thanks!

    Thank you a lot! This works perfectly! Would you like to explain your comment regarding the "almost certainly better solution"?

  4. #4
    Join Date
    Nov 2010
    Posts
    1,084
    one almost certainly better solution would be just to pass the id of the textarea to the function and to grab its value then:

    Code:
    <input type="button" id="btOne" onclick="fnc('tabID','taIn')" value="Press">
    	</input>
    
    function fnc(tableID, textid){
    var text=document.getElementById(textid).value;
    
    //etc
    }

  5. #5
    Join Date
    Apr 2012
    Posts
    6

    Thumbs up

    Sounds reasonable . Thank you!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles