Julian Moreno
Julian Moreno

Reputation: 1104

Auto populate a text field based on another text field (jQuery)

I am trying to auto-populate a text field based on the value of another input field.

The difference between other questions made in Stack Overflow, is:

As showed in this picture: Input field one (Titulo) and input field two (Titulo Alias)

Upvotes: 0

Views: 1216

Answers (3)

arley.wilches
arley.wilches

Reputation: 36

$(document).on('ready', function(){
    $('#titulo').keyup(function() {
        var replacements = {"á":"a", "é":"e", "í":"i", "ó":"o", "ú":"u", "ñ":"n", " ":"-"};
        val = $('#titulo').val().toLowerCase().split('');
        $.each(val, function(i,e){val[i] = replacements[e] ? replacements[e] : e;});
        $('#titulo-alias').val(val.join(''));
    });
});

Upvotes: 2

Leysam Rosario
Leysam Rosario

Reputation: 369

$('#titulo-alias').val( $('#titulo').val().toLowerCase().replace(' ', '-').replace('á', 'a').replace('é', 'e').replace('í', 'i').replace('ó', 'o').replace('ú', 'u').replace('ñ', 'n') );

Upvotes: 0

elclanrs
elclanrs

Reputation: 94101

$('#titulo-alias').val( $('#titulo').val().toLowerCase().replace(' ', '-') );

To remove accents take a look at Remove accents/diacritics in a string in JavaScript

Upvotes: 0

Related Questions