qadenza
qadenza

Reputation: 9293

variable undefined after replacing characters

I want to replace local characters typing in an input

var mapObj = {Č:"C", č:"c", Ć:"C", ć:"c", Đ:"D", đ:"d", Š:"S", š:"s", Ž:"Z", ž:"z"};

function clearlocale(x){
x = x.replace(/č|ć|đ|š|ž/i, function(matched) {
    return mapObj[matched];
});
}

$('#inpnew').keyup(function(e) {
    var a = $(this).val();
    console.log(a); //ok
    var b = clearlocale(a);
    console.log(b); // undefined
});

So why is b - undefined ?

Upvotes: 1

Views: 45

Answers (1)

31piy
31piy

Reputation: 23859

why is b - undefined ?

That's because clearlocale doesn't return anything. Return x after replacement is done.

function clearlocale(x){
  x = x.replace(/č|ć|đ|š|ž/i, function(matched) {
    return mapObj[matched];
  });
  return x;
}

Upvotes: 3

Related Questions