Javascript - mwicat/personal GitHub Wiki

Keyword arguments

Entries.prototype.select = function (
  { from = 0, to = this.length } = {}) {  
  console.log(from);
};

entry.select({ from: 0 });

Inject jquery

javascript:(function() {
    function l(u, i) {
        var d = document;
        if (!d.getElementById(i)) {
            var s = d.createElement('script');
            s.src = u;
            s.id = i;
            d.body.appendChild(s);
        }
    }
    l('//code.jquery.com/jquery-3.3.1.min.js', 'jquery')
})();

jquery version

jQuery.fn.jquery

Get attributes

$.map($("select#product option"), function(a) { return $(a).attr("value") } )

Update dictionary from dictionary

$.extend(d[0].dataset, {'a': 13, 'b': 14})

Iterate array

$.each(arr, function(index, value) {
  alert( index + ": " + value );
});

Iterate object

$.each(obj, function(key, value) {
  alert(key + ": " + value ;
});

Reindent json

vim:

:%!python -m json.tool

Get foreign link

Access-Control-Allow-Origin: <origin> | *

JQuery UI

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<div id="scale"></div>

<script>
	function addScaleButton(num) {
		var clsname = 'scale' + num;
		$('<a />', {
			text : num,
			'class' : clsname
		}).appendTo('#scale');
		$("#scale").find('.' + clsname).button().click(function() {
			alert('hello' + num);
		});
	}

	$(function() {
		for (var i = 1; i <= 8; i++) {
			addScaleButton(i);
		}
	}); 
</script>

##Read synchronously

function read_sync(url) {
  var req = new XMLHttpRequest();  
  var filename = url;

  req.open('GET', filename, false);   
  req.send(null);  
  if (req.status != 0) {
    throw "Cannot open " + filename;
  }
  var xmldoc = req.responseXML;
  return xmldoc;
}

Select box

GET

$('select[name=advertiser]').val()
$('select[name=advertiser] option:selected')
$('select[name=advertiser] option:selected').map(function() { return $(this).attr('value') })
$('select[name=advertiser] option:not(:selected)').map(function() { return $(this).attr('value') })

SET

$('select[name=advertiser] option').attr('selected','selected');
$('select[name=advertiser] option').attr('selected', '');
$('select[name=advertiser] option[value=1925]').attr('selected', '')

Foreach

arr.forEach(function (build) {
});

Get selected val from optionbox

$('[name=resource_type]').find(":selected").val()

Timestamp to date

var timestamp = moment.unix(1293683278);
var time_str = timestamp.format("DD-MM-YYYY HH:mm:ss");
⚠️ **GitHub.com Fallback** ⚠️