Solved

javascript

Posted on 2015-02-21
10
97 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
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 10

Expert Comment

by:Jeffrey Dake
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 44

Expert Comment

by:Rainer Jeschor
Comment Utility
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
Comment Utility
sorry should be as below so it isn't overwriting each time:

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

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 44

Assisted Solution

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

Author Comment

by:Moti Mashiah
Comment Utility
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
Comment Utility
actually, I would like to show each line with number.


thanks ,
0
 
LVL 1

Author Comment

by:Moti Mashiah
Comment Utility
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
Comment Utility
solved
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now