Solved

javascript

Posted on 2015-02-21
10
103 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
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

840 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