Center (proportional font) text in an HTML5 canvas

You can do this by using measureText var canvas = document.getElementById(“canvas”), ctx = canvas.getContext(“2d”) canvas.width = 400; canvas.height = 200; ctx.fillStyle = “#003300″; ctx.font=”20px sans-serif”; var textString = “Hello look at me!!!”, textWidth = ctx.measureText(textString ).width; ctx.fillText(textString , (canvas.width/2) – (textWidth / 2), 100); Live Demo More elaborate demo

code to set a select box option value as an object

You will not be able to store objects/arrays in the value attribute, however an option would be to use data-* attributes which supports json automatically with jQuery 1.4.3+ <select> <option data-value=”{“name”:”rajiv”,”age”:”40″}”>a</option> <option data-value=”{“name”:”mithun”,”age”:”22″}”>f</option> </select> And using .change() $(“select”).change(function(){ alert($(this).find(“:selected”).data(“value”).age); }); Example on jsfiddle

How to sort the select2 (jQuery plugin) options alphabetically?

Select2 API v3.x (sortResults) You can sort elements using sortResults callback option with String.localeCompare(): $( ‘#mylist’ ).select2({ /* Sort data using localeCompare */ sortResults: data => data.sort((a, b) => a.text.localeCompare(b.text)), }); <link rel=”stylesheet” href=”https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.4/select2.min.css” integrity=”sha256-ijlUKKj3hJCiiT2HWo1kqkI79NTEYpzOsw5Rs3k42dI=” crossorigin=”anonymous” /><script src=”https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js” integrity=”sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=” crossorigin=”anonymous”></script><script src=”https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.4/select2.min.js” integrity=”sha256-7A2MDY2eGSSUvgfbuH1IdzYk8qkEd3uzwiXADqPDdtY=” crossorigin=”anonymous”></script> <select name=”list” id=”mylist” style=”width:140px;”> <option>United States</option> <option>Austria</option> <option>Alabama</option> <option>Jamaica</option> <option>Taiwan</option> <option>canada</option> <option>palau</option> … Read more

jQuery find input type (but also for select)

You can do this (fiddle here), make some sort of easy to use plugin: $.fn.getType = function(){ return this[0].tagName == “INPUT” ? this[0].type.toLowerCase() : this[0].tagName.toLowerCase(); } And use it like this $(“.element”).getType(); // Will return radio, text, checkbox, select, textarea, etc (also DIV, SPAN, all element types) $(“.elList”).getType(); // Gets the first element’s type Which … Read more

Calling a javascript function from another .js file

The problem is, that menuHoverStart is not accessible outside of its scope (which is defined by the .ready() callback function in file #1). You need to make this function available in the global scope (or through any object that is available in the global scope): function menuHoverStart(element, topshift, thumbchange) { // … } $(document).ready(function() { … Read more

Change url when manually scrolled to an anchor?

Try using this jquery plugin: Scrollorama. It has tons of cool features and you can use window.location.hash to update your browsers hash. Alternatively, you can add a “scroll” event to check when an anchor is reached. Here is a working fiddle to illustrate the event: http://jsfiddle.net/gugahoi/2ZjWP/8/ Example: $(function () { var currentHash = “#initial_hash” $(document).scroll(function … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)