...

View Full Version : Parse DIV's Value



Tim Conner
07-13-2012, 08:49 PM
How could I parse a div into an array for each new line put that value of the line into an array?

Philip M
07-13-2012, 09:08 PM
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?


<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.

Tim Conner
07-13-2012, 09:33 PM
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;

Old Pedant
07-13-2012, 09:53 PM
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:


var divtext = document.getElementById("mydiv").innerHTML;
var s = divtext.split( /\<br[^\>]*\>/ig );
alert (s[3]); // example

Old Pedant
07-13-2012, 09:58 PM
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.

Philip M
07-14-2012, 06:01 PM
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! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum