mrkeck Sharing the Love

13Jan/150

Make buttons act like buttons

By default, buttons are submits, so this should make them act actually like buttons

$('button').attr('type','button');
Filed under: HTML, jquery No Comments
13Apr/141

jQuery attr vs prop for Checkbox

attr is iffy

$('.checkbox').attr('checked', true);      //Doesn't work
$('.checkbox').attr('checked', 'checked'); //Sometimes works
$('.checkbox').attr('checked', false);     //Works

but prop works every time

$('.checkbox').prop('checked', true);  //Works
$('.checkbox').prop('checked', false); //Works
Filed under: HTML, jquery 1 Comment
25Feb/140

What is being clicked?

Sometimes with onclick stuff isn't happening as expected so I do

$(document).on('click', function (e) {
	console.log(e.target);
});

which tells me what is receiving the click

Filed under: jquery No Comments
6Jan/140

Select2 Width not listening to CSS

When the select2 doesn't change with the css, use $('select').select2({width:'resolve'});
resolve tells it use the css and if there isn't any then guess using javascript.
http://ivaynberg.github.io/select2/#documentation


Ex.

<style>
select{
width: 500px;
}
</style>
<select>
<option>Some Option</option>
</select>
<script>
$('document').ready(function(){
	$('select').select2({width:'resolve'});
});
</script>
Filed under: jquery, select2 No Comments
2Dec/130

jQuery Submit Function Not Working

jQuery submit function error "Property 'submit' of object # is not a function" seems to happen if you use "submit" as a name inside the form you are trying to use the submit() function on.
Ex.

<form id="theFrm">
<button name="submit" type="submit">Save</button>
</form>

Don't use "submit" as a name and $("#theFrm").submit() should work

Filed under: jquery No Comments
19Jun/130

jQuery Uppercase each

$('.model').each(function(i,v){$(this).text($(this).text().toUpperCase());});
Filed under: jquery No Comments
20Apr/130

Use Latest jQuery

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
Filed under: Javascript, jquery No Comments
1Dec/120

jQuery Full Position of Element on Page

var eleBounds=function(ele){
	ele=$(ele);
	var off=ele.offset();
	var bounds={};
	bounds.top=off.top;
	bounds.bottom=bounds.top+ele.height();
	bounds.left=off.left;
	bounds.right=bounds.left+ele.width();
	bounds.height=ele.height();
	bounds.width=ele.width();
	return bounds;
}

Works well with phantomJS clipRect, just have to get the function in the page and return back the object and set clipRect equal to it.

Filed under: jquery, phantomJs No Comments
26Nov/120

Javascript sleep function

var sleepyTime=10*1000;  //10 seconds
setTimeout(function(){
//In 10 seconds this will alert("I'm awake!");
alert("I'm awake!");
},sleepyTime);
4Aug/120

Simulate Mouse Click with Javascript/jQuery

//Find thing you want to click
var ele=$('.thingToClick')[0];
 
//Function that performs the click
var doClick=function(ele){
  var evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window,
	0, 0, 0, 0, 0, false, false, false, false, 0, null);
  return ele.dispatchEvent(evt);
};
 
//Call the click
doClick(ele);
Filed under: Javascript, jquery No Comments