Reputation: 5
How can I get the Weight: & the Weight with box: into 2 variables so I can use elsewhere
The main problem is I can't simply count the number of br's and work off that as they are inconsistent, some will have 4 some will have 6/7 as the information before the weights change... the only thing consistent is the weights and containing class
<div class="box">
Quantity: 1 <br>
Quantity pack: 10 <br>
Box (width x length x height): 65x65x120 <br>
Quantity 2: 2083 <br>
Pallet : 800x1200x1100 <br>
Weight: 95 <br>
Weight with box: 115 <br>
</div>
Thanks Guys
Upvotes: 0
Views: 74
Reputation: 337550
The structure of the HTML is not ideal for extracting data. I would suggest you amend it to something more suitable, if possible. If it is not, you can retrieve all text nodes, and then get the final two values - assuming the format of the HTML does not change. Something like this:
var nodes = $('.box').contents().filter(function() {
return this.nodeType == 3 && $.trim(this.textContent) != '';
});
var weight = nodes[nodes.length -1];
var weightBox = nodes[nodes.length -2];
If the order of the properties is not consistent, you would need to loop through the nodes
array and check for the preceeding weight:
or weight with box:
text.
Upvotes: 2
Reputation: 10771
Quick and dirty solution would be to wrap them with something you can identify them with, notice the spans with the ID that you can use to target the nodes with.
<div class="box">
Quantity: 1 <br>
Quantity pack: 10 <br>
Box (width x length x height): 65x65x120 <br>
Quantity 2: 2083 <br>
Pallet : 800x1200x1100 <br>
Weight: <span id="weight">95</span> <br>
Weight with box: <span id="weight-box">115</span> <br>
</div>
<script>
var weight = document.getElementById("weight").innerHTML;
</script>
See this JS fiddle. http://jsfiddle.net/8jduxn1s/
Upvotes: 0