Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Text to Array Question

    I'm creating a page with a text box and a button. I was wondering if it was possible to make it so that when the button is clicked the text that was put into the text box is converted into an array and the content is displayed on the page. How would I go about doing this?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Gibby1 View Post
    I'm creating a page with a text box and a button. I was wondering if it was possible to make it so that when the button is clicked the text that was put into the text box is converted into an array and the content is displayed on the page. How would I go about doing this?
    What does that mean?

    Does this give you any assistance?


    Code:
    <input type = "text" id = "mytext">
    <br>
    <input type = "button" value = "Show this text on the page" onclick = "show()">
    <br><br>
    <span id = "myspan"></span>
    
    <script type = "text/javascript">
    
    function show() {
    var txt = document.getElementById("mytext").value;
    document.getElementById("myspan").innerHTML = txt;
    }
    
    </script>
    Quizmaster: "I'm just going outside, and may be some time" were the last words of which explorer?
    Contestant: Columbus

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Gibby1 (03-30-2013)

  • #3
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I think this is close. What I mean is - is there a way to display what's typed as an array. Like if I was to type "Lets Go Blue" into the text box it would return as, "0-Lets, 1-Go, 2-Blue"?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Gibby1 View Post
    I think this is close. What I mean is - is there a way to display what's typed as an array. Like if I was to type "Lets Go Blue" into the text box it would return as, "0-Lets, 1-Go, 2-Blue"?
    Here you are (although I don't really see the point of it):-

    Code:
    <input type = "text" id = "mytext">
    <br>
    <input type = "button" value = "Show this text on the page" onclick = "show()">
    <br><br>
    <span id = "myspan"></span>
    
    <script type = "text/javascript">
    
    function show() {
    var message = "";
    var txt = document.getElementById("mytext").value;
    if (txt != "") {
    var txtsplit= txt.split(" ");
    var len = txtsplit.length;
    for (var i = 0; i < len; i++) {
    message += i + "-" + txtsplit[i] + " " ;
    }
    }
    
    document.getElementById("myspan").innerHTML = message;
    }
    
    </script>
    Last edited by Philip M; 03-30-2013 at 07:27 PM. Reason: Typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Gibby1 (03-30-2013)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Add this line in blue as shown:-

    var txt = document.getElementById("mytext").value;
    txt = txt.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
    if (txt != "") {

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •