Solved

javascript

Posted on 2015-02-21
10
104 Views
Last Modified: 2015-03-03
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 ,
0
Comment
Question by:Moti Mashiah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 10

Expert Comment

by:Jeffrey Dake
ID: 40623241
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
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 40623252
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

0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40623418
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 ,
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 40623471
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
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 250 total points
ID: 40623472
sorry should be as below so it isn't overwriting each time:

...
txtare.value = textare.value + (start + "<br/>");
...

Open in new window

0
 
LVL 44

Assisted Solution

by:Rainer Jeschor
Rainer Jeschor earned 250 total points
ID: 40623477
And here an online sample:
http://jsfiddle.net/EE_RainerJ/v51Lj7tL/
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40623512
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

0
 
LVL 1

Accepted Solution

by:
Moti Mashiah earned 0 total points
ID: 40623513
actually, I would like to show each line with number.


thanks ,
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40623517
k, I found the solution regarding the line

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


thank you.
0
 
LVL 1

Author Closing Comment

by:Moti Mashiah
ID: 40641417
solved
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Timer on div 5 24
Find out all repos that a user is most active on Github 1 32
Want an individual results display div 8 46
GitHub 1 10
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question