Link to home
Start Free TrialLog in
Avatar of Moti Mashiah
Moti MashiahFlag for Canada

asked on

javascript

Hi guys,

I'm really beginner to javascript and I would like to know how do I write some result to the textarea.

Here is what I'm trying to do:

        function lopwhil() {
            var target = Number(prompt("Please enter number max (100)"))
            var txtare = document.getElementById("numd").value;
            var wrto = start + "<br/>";
            var start = 0;
            while (start <= target) {
                document.write(start + "<br/>");
                start = start + 2;
                
                if (start > 100)
                    break;
            }
        }

Open in new window


so instead to write to "document.write(start + "<br/>"); i would like to write the result to the variable I prepared which is my textarea - "var txtare = document.getElementById("numd").value;"

thanks ,
Avatar of Jeffrey Dake
Jeffrey Dake
Flag of United States of America image

I would really recomend using jquery. There are a lot of functions already written to do what you are trying to do.  It is also a very commonly used library and well supported. Here is an example of using the val() function to get and set values.

http://www.jquerytutorials.net/jquery-textarea.html
change variable textare so it refers to the textarea rather than the textarea value:

var txtare = document.getElementById("numd").value;

Open in new window


to :
var txtare = document.getElementById("numd");

Open in new window


then change the document write :
...
            while (start <= target) {
                textare.value = start + "<br/>";
...

Open in new window


so you end up with:

function lopwhil() {
            var target = Number(prompt("Please enter number max (100)"))
            var txtare = document.getElementById("numd");
            var wrto = start + "<br/>";
            var start = 0;
            while (start <= target) {
                textare.value = start + "<br/>";
                start = start + 2;
                
                if (start > 100)
                    break;
            }

Open in new window

Avatar of Moti Mashiah

ASKER

I have changed the code as your suggestion and I got just one value in the textarea. for example in the prompt window I typed 50 and entered so in this case i should get back the number 2,4,6,8 until 50 so i got just 50<br/> to the textarea,

Now i was trying to loop it and get the same result.

function lopech() {
            var target = Number(prompt("Please enter number max (100)"))
            var txtare = document.getElementById("numd");

            for(var start = 0; start <= target; start = start + 2 ){
                txtare.value = (start + "<br/>");

                if (start > 100)
                    break;
            }

        }

Open in new window


please help thanks ,
Hi,
this is due to the fact that you always overwrite the textarea.
This should fix it:
function lopech() {
            var target = Number(prompt("Please enter number max (100)"))
            var txtare = document.getElementById("numd");

            for(var start = 0; start <= target; start = start + 2 ){
                txtare.value = (""+ txtare.value + start + "<br/>");

                if (start > 100)
                    break;
            }

        }

Open in new window


HTH
Rainer
SOLUTION
Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi guys ,

thank you for you solution it is working now, just the result show also the break

like:
0<br/>2<br/>4<br/>6<br/>8<br/>10<br/>12<br/>14<br/>16<br/>18<br/>20<br/>22<br/>24<br/>26<br/>28<br/>30<br/>32<br/>34<br/>36<br/>38<br/>40<br/>42<br/>44<br/>46<br/>48<br/>50<br/>

Here is the code:
 function lopech() {
            var target = Number(prompt("Please enter number max (100)"));
            var txtare = document.getElementById("numd");

            for (var start = 0; start <= target; start = start + 2) {
                txtare.value = ("" + txtare.value + start + "<br/>");

                if (start > 100)
                    break;
            }

        }

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
k, I found the solution regarding the line

txtare.value = (""+ txtare.value + start + "\n");


thank you.
solved