I have the following script which works great however I think this can be simplify, right? also, I'd like it to trigger only once.
Example, user inputs "My Title". Script executes and user sees "my-title" in slug field. If user replaces title value by "My Crazy Title", I'd like the slug to remain "my-title". How?
$('#article_title').change(function() {
str = $('#article_title').val();
formatted = str.replace(/\s+/g, '-').toLowerCase();
$('#article_slug').val(formatted);
});
See code example http://jsfiddle.net/TSrYu/
You can simplify it this way:
.one()
to register your event handler so it only fires once.The code:
$('#article_title').one('change', function() {
$('#article_slug').val($(this).val().replace(/\s+/g, '-').toLowerCase());
});
Working demo: http://jsfiddle.net/jfriend00/XGjWA/