oratis
oratis

Reputation: 828

How to get a textarea's value and put it into another textarea using Javascript?

I am trying to develop a url decoder using Javascript but my code won't work, can anyone tell me how to do it,thx

<textarea id="input"></textarea>
<br />
<button id="yes" align="center" onclick="getInput(document.getElementById('input')">Click to transfer</button>
<p>The decoded URL:</p>
<textarea id="decode"></textarea>
<script>
    function getInput(inputElement) {

        var decoded = "";

        if inputElement == 0 {

            document.write("You didn't input anything");

        } else if {

            var uri = inputElement;
            var uri_decode = decodeURIComponent(uri);
            var uri_encode = encodeURIComponent(uri)

            document.getElementById("decode").innerHTML = uri_decode;
            document.getElementById("encode").innerHTML = uri_encode;

            decoded = uri_decode;

        }
        return decoded;
    }
</script>

Upvotes: 0

Views: 6634

Answers (4)

Wim
Wim

Reputation: 85

even though im still quite new to this, i think i found a few mistakes, im not sure if it will fix you're problem, but you should give it a try.

1. <script>

would need to be:

<script type="text/javascript">

otherwise your browser doesnt know what type of script it needs.

2.if inputElement == 0 {

would need to be: " if (inputElement == 0) {

3.} else if {

should be } else {

closing the script stays the same : </script>

i hope this works, if it doesn't then tell me.

Upvotes: 1

Adam92
Adam92

Reputation: 436

My suggestion would be to use the jQuery Api.

Once you click a button you can pick it up using

$('#transfer').click(function() { }

Then simply collect the text from the first text box using

var url = $('#urlToDecode').val();

then write the 'url' variable into the second text box using

$("#decodedURL").val = url;

Hope that helps! :) Note: you will need to link to the jQuery API which is hosted by Google.

Upvotes: 0

Yash
Yash

Reputation: 177

Assuming logic is correctly done in your original code.

Since it is a text area,you must use value

Try :

document.getElementById("decode").value = uri_decode;

More Properties at HERE

Upvotes: 1

BlueCacti
BlueCacti

Reputation: 10820

In your else if, you didn't specify an if-statement. Guess you meant else

Upvotes: 0

Related Questions