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:
[code]this.nodeName.toLowerCase()[/code]

This code will see if we have a map set of inputs (somename[]):
[code]// See if we have a map here
var mapFound = 0;
if (thisName.indexOf(‘[]’) != -1) {
mapFound = 1;
thisName = thisName.replace(/[\[\]’]+/g, ”);
}[/code]

How to take a map of inputs and turn them into a Javascript map type var, and toss the empty ones:
[code]var map = $(thisName).map(function(){
if (this.value != ”) {
return this.value;
} else {
return null;
}
}).get();[/code]

jQuery and Form Submit Breaking

May 10th, 2012

If you intercept the form submit with some jQuery and find that you can’t trigger the submit yourself with your script, you may need to check the name of your submit button. If you have a submit button named submit, jQuery will not be able to submit that form. You may also see the error “Uncaught TypeError: Property ‘submit’ of object # is not a function”.

The fix is simple of course. Change the button name.

Wrong: [code]<input type="submit" name="submit" id="confirm" value="Confirm">[/code]
Right: [code]<input type="submit" name="confirm" id="confirm" value="Confirm">[/code]

Hope that helps someone else.

WordPress - Entries (RSS) and Comments (RSS) - © 2011 Ben Dauphinee