I have a code that I create to detect if a page contains a 'table' element and if so add some elements to those tables as a message if there is no table and a different message with a link if it exists. In addition to adding a div
using the wrap()
method to optimize the mobile table.
Everything works well but I would like to see how to optimize it because I feel that there are several things that are repeated for example if (table_exist)
is repeated 3 times. This is the code:
jQuery(document).ready(function($) {
var table_exist = ($('table').length);
var elTitulo = $(".entry-title");
$("table").before("<div id='tablesp'></div>");
if (table_exist) {
if($('table tbody tr').length < 4){
// Si hay mesa con menos de 4 lineas
elTitulo.after("<a href='#tablesp' class='speclink'>Ver BREVE Tabla de Especificaciones</a> ");
} else {
elTitulo.after("<a href='#tablesp' class='speclink'>Ver Tabla de Especificaciones</a> ");
}
} else {
elTitulo.after("<p class='speclink'>(No hay tabla especial de especificaciones. Breve descripción disponible)</p> ");
}
//Enlace bajo la tabla para regresar a header
if (table_exist)
{
$('table').after("<a href='#left-area' class='speclink2'>Regresar arriba</a> ");
} else {
$('.single #main-content').append("<a href='#left-area' class='speclink3'>Regresar arriba</a> ");
}
//Agregar div a tables para optimizar en mobiles
if (table_exist)
{
$('table').wrap("<div style='overflow-x:auto;'>");
}
});