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 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Parse DIV's Value

    How could I parse a div into an array for each new line put that value of the line into an array?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    Quote Originally Posted by Tim Conner View Post
    How could I parse a div into an array for each new line put that value of the line into an array?
    Clear as mud! Is this what you mean?

    Code:
    <div id = "mydiv">Lorem veggies sunt bona vobis, <br>
    proinde vos postulo esse magis burdock groundnut <br>
    salad bell pepper fennel turnip greens cabbage desert <br>
    raisin caulie squash. burdock horseradish epazote <br>
    asparagus broccoli taro avocado kohlrabi wattle seed <br>
    sweet pepper water spinach arugula bunya nuts grape. </div>
    
    <script type = "text/javascript">
    
    var divtext = document.getElementById("mydiv").innerHTML;
    divtext = divtext.replace(/<br>/gi,"~");
    var s = divtext.split("~");
    alert (s[3]);  // example
    
    </script>

    It is not as cold as it was yesterday, but that's probably because it's a bit warmer. - BBC Scotland News.

    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.

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry thought I explaioned it. Ok so you have a div lets say it looks like this
    12
    13
    14
    I want to get each value and put it into an array so it would be like
    var number=new Array();
    number[0]=12;
    number[1]=13;
    number[2]=14;

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    Obviously you didn't bother to try Philip's code.

    He *GAVE* you the answer.

    Excepting that he limited the line delimiters to just <br> whereas they could of course be <br /> or <br/> or ... Or even, I suppose, <p>...</p>.

    Assuming, though, that the delimiters are some form of <br ...> you could do this:
    Code:
    var divtext = document.getElementById("mydiv").innerHTML;
    var s = divtext.split( /\<br[^\>]*\>/ig );
    alert (s[3]);  // example
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    Clearly Philip knew something I didn't. Even if you code using <br/>, innerHTML will apparently just show them as <br>, so he was right. Sorry for doubting you, Philip.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    Quote Originally Posted by Old Pedant View Post
    Clearly Philip knew something I didn't. Even if you code using <br/>, innerHTML will apparently just show them as <br>, so he was right. Sorry for doubting you, Philip.
    No problem!

    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
    •