Javascript Element Properties From Object

December 19th, 2013

How to create a HTML tag, using an object of properties.

$('#somediv').append($('<a/>', {href: '#', id: 'dothing', text: 'Do Thing'}));

Javascript HTML Fun

December 18th, 2013

Here are some useful Javascript snips for working with form data.

How to get the name of the HTML tag for an element:

this.nodeName.toLowerCase()

This code will see if we have a map set of inputs (somename[]):

// See if we have a map here
var mapFound = 0;
if (thisName.indexOf('[]') != -1) {
    mapFound = 1;
    thisName = thisName.replace(/[\[\]']+/g, '');
}

How to take a map of inputs and turn them into a Javascript map type var, and toss the empty ones:

var map = $(thisName).map(function(){
    if (this.value != '') {
        return this.value;
    } else {
        return null;
    }
}).get();
WordPress - Entries (RSS) and Comments (RSS) - © 2011 Ben Dauphinee