Solved

javascript

Posted on 2015-02-21
10
99 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

911 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

20 Experts available now in Live!

Get 1:1 Help Now