Search

stitchzaa

Some useful code & information for web dev (HTML5, CSS3, JavaScript) and iOS app dev (Swift)

Category

JavaScript

jQuery: new element and insert element.

How to create a new element in jQuery:

var unreadMarker = $('<a>', {
  "class": "unread-marker"
});

OR

var unreadMarker = $('<a class="unread-marker"></a>');

How to insert element:

a.after(b) ==>  <a>  <b>

a.insertAfer(b) ==> <b> <a>

a.append(b) ==> <a> xx yy b </a>

a.prepend(b) ==> <a> b xx yy </a>

Use regular expression to filter telephone number in JavaScript

function checkPhoneNumber(phone) {
    var regexp = /^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/i
    if (regexp.test(phone) && phone.length > 0) {
        return true;
    } else {
        return false;
    }
}

Continue reading “Use regular expression to filter telephone number in JavaScript”

Data-binding

Link of information about Data-binding Revolutions with Object.observe()

http://www.html5rocks.com/en/tutorials/es7/observe/

How to combine 2 canvas to one

You can combine 2 canvas elements onto 1 or the third canvas element by using drawImage().

About the drawImage method :

  • This method is used when you want to use images on the canvas.
  • The drawImage method can also draw parts of an image, and/or increase/reduce the image size and can take up to nine parameters, depending on what you want to do with the image.
  • The image object can be an image, a video, or another canvas element.
This is an Example of using drawImage() with 2 canvas Element
var can = document.getElementById('canvas');
var ctx = can.getContext('2d');

var can2 = document.getElementById('canvas2');
var ctx2 = can2.getContext('2d');

ctx2.drawImage(can, 0, 0);

Create a free website or blog at WordPress.com.

Up ↑