Javascript - add text area value - IE Chrome and Firefox

How to do it. I am trying to add text to a text area. If some value already exists, add. It worked for me, but it looks like it cannot work in all browsers. Is there a better way to do this?

Tried both methods, they do not work in Chrome, IE and FF -

if ($('#log').value == undefined) {
$('#log').val("First: " + result[0]);
} else {
$('#log').val($('#log').value += "Second: " + result[0]);
}

if ($('#log').value == undefined) {
$('#log').val("First: " + result[0]);
} else {
$('#log').val(log.value += "Second: " + result[0]);
}

Without If, this works in Chrome, but not in IE and FF -

$('#log').val(log.value += "First: + result[0]);
+3
source share
1 answer

JQuery objects do not have a property .value, so $('#log').valuethere always will be undefined. This means that your statement will always call $('#log').val("First: " + result[0]);and will never call the second.

Instead, check if($('#log').val() == '').

.val(), . .

if ($('#log').val() == '') {
    $('#log').val("First: " + result[0]);
} else {
    $('#log').val(function(i, v){ // v is the current value
        return v + "Second: " + result[0];  // what returned is what the value will be set to
    });
}
+2

All Articles