Recursive functions can be very effective in manipulating tree structures such as the browser’s Document Object Model (DOM). Each recursive call is given a smaller piece of the tree to work on.
// Javascript - The Good Parts page: 35
var walkTheDom, getElementsByAttribute;
Define a walkTheDom function that visits every node of the tree in HTML source order, starting from some given node. It invokes a function, passing it each node in turn. walkTheDom calls itself to process each of the child nodes.
walkTheDom = function walk(node, funk) {
funk(node);
node = node.firstChild;
while(node) {
walk(node, funk);
node = node.nextSibling;
}
};
Define a getElementsByAttribute function. It takes an attribute name string and an optional matching value. It calls walkTheDom, passing it a function that looks for an attribute name in the node. The matching nodes are accumulated in a results array.
getElementsByAttribute = function(att, value) {
var results = [];
walkTheDom(document.body, function(node) {
var actual = node.nodeType === 1 && node.getAttribute(att);
if (typeof actual === 'string' &&
(actual === value || typeof value !== 'string')) {
results.push(node);
}
});
return results;
};
Top Answer!
If you are at a certain branch mybranch, just go ahead and git checkout commit_hash. Then you can return to your branch by git checkout mybranch. I had the same game bisecting a bug today :)
Also, you should know about git bisect.
I could just hit nodejs.org and get the new image, but figured there had to be an easier way. It turns out there is -- you can upgrade your local Node.js with NPM:
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
The n package represents a Node helper, and running the last command upgrades node to the latest stable version. Instead of using stable, you could specify a desired version:
sudo n 0.12.2
Once your install is complete, you can confirm you version with another command:
node -v
You’ve decided that you’re going to work on issue #53 in whatever issue-tracking system your company uses. To create a branch and switch to it at the same time, you can run the git checkout command with the -b switch:
$ git checkout -b iss53
Switched to a new branch "iss53"
This is shorthand for:
$ git branch iss53
$ git checkout iss53
Switch back to your master branch:
$ git checkout master
Switched to branch 'master'
Next, you have a hotfix to make. Let’s create a hotfix branch on which to work until it’s completed:
$ git checkout -b hotfix
Switched to a new branch 'hotfix'
$ vim index.html
$ git commit -a -m 'fixed the broken email address'
[hotfix 1fb7853] fixed the broken email address
1 file changed, 2 insertions(+)
You can run your tests, make sure the hotfix is what you want, and merge it back into your master branch to deploy to production. You do this with the git merge command:
$ git checkout master
$ git merge hotfix
Updating f42c576..3a0874c
Fast-forward
index.html | 2 ++
1 file changed, 2 insertions(+)
After your super-important fix is deployed, you’re ready to switch back to the work you were doing before you were interrupted. However, first you’ll delete the hotfix branch, because you no longer need it – the master branch points at the same place. You can delete it with the -d option to git branch:
$ git branch -d hotfix
Deleted branch hotfix (3a0874c).
To remove a remote branch (if you know what you are doing!)
git push origin :hotfix
more at git-scm.com
Get list of all 'input' objects using JavaScript, without accessing a 'form' object.
function getFormData(formID) {
var container, inputs, len, index, data = {};
// get the container element
container = document.getElementById(formID);
// find it's child 'input' elements
inputs = container.getElementsByTagName('input');
for(index = 0, len = inputs.length; index < len; index++) {
if(inputs[index].name) {
data[inputs[index].name] = inputs[index].value;
}
}
return data;
}
Test with this.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="" id='my-form'>
<input type="text" name="user" id="user" value="tommy" />
<input type="text" name="pass" id="pass" value="qwerty" />
<input type="submit" value="GO">
</form>
</body>
</html>