James
James

Reputation: 43667

Get two values from one with jQuery

<div>345000000, 46400000000</div>

How do I make two values from the text of this <div>? They are divided by comma.

We should get var value_1 = 345000000 and var value_2 = 46400000000

Upvotes: 2

Views: 139

Answers (4)

Levi Morrison
Levi Morrison

Reputation: 19552

Answer: http://jsfiddle.net/morrison/F38FC/

Notes:

  • Shows how to grab multiple divs.
  • Uses JavaScript's String.split().
  • Open the console to view the pieces.

Upvotes: 1

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18354

 <div id="numbers">345000000, 46400000000</div>
 <script>   
    $(document).ready(function(){
      var parts = $("#numbers").html().split(",");
      var value1 = parseInt(parts[0], 10);
      var value2 = parseInt(parts[1], 10);
      alert(value1);
      alert(value2);
    });
 </script>

Hope this helps. Cheers

Upvotes: 3

Thomas Shields
Thomas Shields

Reputation: 8943

Grab the value, split 'em up.

<div id="nums">12, 15</div>

var str=document.getElementById("nums").innerHTML;
var nums = str.split(",");
alert(nums[0]); //gets first number

Upvotes: 2

David Fells
David Fells

Reputation: 6798

Look up Javascript's string.split() function.

Upvotes: 2

Related Questions