The curious case of a textarea

May 19, 2011

I came across this little problem today, where I wanted the modified content of a textarea.

This is the textarea:

<textarea id="area">Some text</textarea>

I wanted to edit it, and then alert the result. But guess what, the alerted text said “Some text” and not “Edited text”

The following code was tried:

alert($('#area').text());
alert($('#area').html());
alert(document.getElementById('area').innerHTML);

The answer was really simple after some minutes of trying, but the question why still remains.

This does the trick:

alert(document.getElementById('area').value);
alert($('#area').val());

Advertisements

try to catch this if else

May 1, 2011

How should you write “try catch” and “if else”?

I’ve seen a lot of this:

if() {
}
else {
}

try {
}
catch {
}

But it really should be like:

if() {
} else {
}

try {
} catch {
}

Why? It’s really a little detail, but in the first examples it looks like you could add code between the try and the catch, because they are two blocks of code. But the catch belongs to the try, so it should not be seperated by a new line.