JavaScript javascript split text into list of words and then dynamically added to div, form tag messing it up javascript split text into lines,javascript split text by newline,

I was testing this code out in an asp.net webform as I am looking to split a text into a list or words in javascript and have each added to its own div.

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_split

However in an asp.net webform, the sample code alone produces strange behavior, and the result appears and disappears. Simply adding the form tags to the online sample reproduces the behavior.

<form id="form1" runat="server">

</form>

Is there a workaround for this or alternative ideas to do this in javascript?

Thanks

Answer:1

The behavior you are experiencing is happening because the button is triggering a postback to the server. This essentially reloads the page which is why it appears as though the text is appearing then disappearing right away.

You can fix this by adding in a return false; after onclick="myfunction()" like this:

<form id="form1" runat="server">
    <p>Click the button to display the array values after the split.</p>

    <button onclick="myFunction();return false;">Try it</button>

    <p id="demo"></p>

    <script>
        function myFunction() {
            var str = "How are you doing today?";
            var res = str.split(" ");
            document.getElementById("demo").innerHTML = res;
        }
    </script>
</form>

However, I do not recommend using the onclick attribute to call a JavaScript function as there are better ways to do this. For the purpose of your example, I provided the simplest solution to the problem, but I want to make you aware that there are better ways to handle this.

Answer:2

new javascript student here looking for a little clarification. I keep running into problems involving For Loops/Arrays. I'm not familiar enough with the concepts to diagnose my own problems. The ...

new javascript student here looking for a little clarification. I keep running into problems involving For Loops/Arrays. I'm not familiar enough with the concepts to diagnose my own problems. The ...

  1. return string in c
  2. return string in java
  3. return string in python
  4. return string in javascript
  5. return string in c function
  6. return string in actionresult
  7. return string in cpp
  8. return string in python function
  9. return string in php
  10. return string in async task c#
  11. return string with variables python
  12. return string and int java
  13. return string in shell script
  14. return string in json format c#
  15. return string in javascript function
  16. return string to view mvc
  17. return string in js
  18. return string in bash
  19. return string in java method
  20. return string and int python

I am trying to post some data to a php file by using following code. var exam = document.getElementById("information").getAttribute("examid"); var user = document.getElementById("...

I am trying to post some data to a php file by using following code. var exam = document.getElementById("information").getAttribute("examid"); var user = document.getElementById("...

  1. post value in php
  2. post value in pega
  3. post value of checkbox
  4. post value to another page php
  5. post value in ajax
  6. post value in codeigniter
  7. post value in javascript
  8. post value in url
  9. post value of select option
  10. post value of disabled input
  11. post value of unchecked checkbox
  12. post value in jquery ajax
  13. post value to controller ajax
  14. post value in php without form
  15. post value in jquery
  16. post value of button
  17. post value in html
  18. post value in laravel
  19. post value of submit button
  20. post value of checkbox php

I am developing a web page in which a user can use a barcode scanner to scan a QR code. I am using the following code to capture the contents of the QR code: $(document).ready(function () { var ...

I am developing a web page in which a user can use a barcode scanner to scan a QR code. I am using the following code to capture the contents of the QR code: $(document).ready(function () { var ...

I would like to ask a question about event handlers when used in HTML and React. In the book Javascript and Jquery written by Jon Duckett, the author mentioned that using HTML event handler attribute ...

I would like to ask a question about event handlers when used in HTML and React. In the book Javascript and Jquery written by Jon Duckett, the author mentioned that using HTML event handler attribute ...

  1. html event handler attributes
  2. html event handler javascript
  3. html event handler
  4. html event handler return value
  5. html event handler parameters
  6. html event handler list
  7. html event handler arguments
  8. html event handler xss
  9. html event handler dropdown
  10. html event handler input
  11. html event handler remove
  12. html click event handler
  13. html inline event handler
  14. html button event handler
  15. html checkbox event handler
  16. html select event handler
  17. html add event handler
  18. html onclick event handler
  19. html form event handler
  20. html submit event handler