Skip to content

Instantly share code, notes, and snippets.

@zevilz
Last active July 16, 2019 14:01
Show Gist options
  • Save zevilz/82942e25ffd3f88837d21a50a28ae3fa to your computer and use it in GitHub Desktop.
Save zevilz/82942e25ffd3f88837d21a50a28ae3fa to your computer and use it in GitHub Desktop.
cf_seo_progressbars
function getProgressClass(type, value) {
if ( type == 'meta_t' ) {
if (value < 400) {
var progress_class = 'ok';
}
else if (value > 600) {
var progress_class = 'bad';
}
else {
var progress_class = 'good';
}
}
else if ( type == 'meta_d' ) {
if (value < 121) {
var progress_class = 'ok';
}
else if (value > 156) {
var progress_class = 'ok';
}
else {
var progress_class = 'good';
}
}
return progress_class;
}
jQuery(document).ready(function($) {
if ($('input[name="_meta_t"]').length > 0) {
$('input[name="_meta_t"]').attr('placeholder', $('#title').val());
if ($('input[name="_meta_t"]').val().length > 0) {
$('input[name="_meta_t"]').after('<progress class="seo_progress ' + getProgressClass('meta_t', $('input[name="_meta_t"]').val().length * 9) +'" value="' + $('input[name="_meta_t"]').val().length * 9 + '" max="600"></progress>');
}
else {
$('input[name="_meta_t"]').after('<progress class="seo_progress ' + getProgressClass('meta_t', $('input[name="_meta_t"]').attr('placeholder').length * 9) +'" value="' + $('input[name="_meta_t"]').attr('placeholder').length * 9 + '" max="600"></progress>');
}
$('input[name="_meta_t"]').on('change keyup input click', function() {
if ($(this).val().length > 0) {
$(this).next().val($(this).val().length * 9);
var progress_class = getProgressClass('meta_t', $(this).val().length * 9);
}
else {
$(this).next().val($(this).attr('placeholder').length * 9);
var progress_class = getProgressClass('meta_t', $(this).attr('placeholder').length * 9);
}
if (!$(this).next().hasClass(progress_class)) {
$(this).next().removeClass('ok');
$(this).next().removeClass('bad');
$(this).next().removeClass('good');
$(this).next().addClass(progress_class);
}
});
$('#title').on('change keyup input click', function() {
$('input[name="_meta_t"]').attr('placeholder', $(this).val());
if ($('input[name="_meta_t"]').val().length == 0) {
$('input[name="_meta_t"]').next().val($('input[name="_meta_t"]').attr('placeholder').length * 9);
var progress_class = getProgressClass('meta_t', $('input[name="_meta_t"]').attr('placeholder').length * 9);
if (!$('input[name="_meta_t"]').next().hasClass(progress_class)) {
$('input[name="_meta_t"]').next().removeClass('ok');
$('input[name="_meta_t"]').next().removeClass('bad');
$('input[name="_meta_t"]').next().removeClass('good');
$('input[name="_meta_t"]').next().addClass(progress_class);
}
}
});
}
if ($('textarea[name="_meta_d"]').length > 0) {
$('textarea[name="_meta_d"]').after('<progress class="seo_progress ' + getProgressClass('meta_d', $('textarea[name="_meta_d"]').val().length) +'" value="' + $('textarea[name="_meta_d"]').val().length + '" max="156"></progress>');
$('textarea[name="_meta_d"]').on('change keyup input click', function() {
$(this).next().val($(this).val().length);
var progress_class = getProgressClass('meta_d', $(this).val().length);
if (!$(this).next().hasClass(progress_class)) {
$(this).next().removeClass('ok');
$(this).next().removeClass('bad');
$(this).next().removeClass('good');
$(this).next().addClass(progress_class);
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment