-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
45 lines (38 loc) · 1.42 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* global $: false, _: false */
(function () {
'use strict';
$(document).ready(function(){
var $input,
sum = 0,
url = 'http://dev.markitondemand.com/Api/v2/Quote/jsonp/?symbol=';
//REMOVE ROW
$('#stockInfo').on('click', '.delete', function(event){
$(this).closest('tr').remove();
event.preventDefault();
});
//SUBMIT EVENT
$(".form").submit(function(event){
event.preventDefault();
var symbol = $(".symbol").val();
var symbolUrl = url + symbol;
//GET DATA FUNCTION
$.get(symbolUrl, function(data){
var quantity = $(".quantity").val();
var change = Math.round(data.Change * 100) / 100;
var changePercent = Math.round(data.ChangePercent * 100) / 100;
var price = Math.round(data.LastPrice * 100) / 100;
//APPEND NEW ROW
var $tr = $('<tr><td>' + data.Name + '</td><td>'
+'$' + price + '</td><td>'
+ change + ' / ' + changePercent + '%' + '</td><td>'
+ quantity + '</td><td>'
+ '<input type="submit" value="Remove" class="button delete">' + '</td></tr>');
$('#stockInfo').append($tr);
//FIND TOTAL PRICE
sum += parseFloat(price) * parseInt(quantity);
var $total = ('<div>' + 'Total: ' + sum.toFixed(2) + '</div>');
$('.total').empty().append($total);
}, 'jsonp');
});
});
})();