Insert Event date into a table if page width is less than a given value

Asked

Viewed 29 times

1

What I want to do is when the width page is less than the value shown below it enters data-tablesaw-mode="stack on the site shown below! I am new in JavaScript but I need this for a project! Thank you to whom I help I am available to provide any information that is important.

What I intend to insert

 data-tablesaw-mode="stack"

Like I’m trying to do

<script>
var width = (window.innerWidth > 0) ? window.innerWidth : screen.width;
var table = document.getElementsById('table-hide');

if width<39.9375em {
   table. += "data-tablesaw-mode="stack";
}
</script>

HTML where I want to insert

<table id="table-hide" class="tablesaw tablesaw-stack">

2 answers

2


To work as desired, you must put the document to listen to the event resize, create a function:

function screen_width() {
    var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    if(width < 400) {
        // Adiciona o atributo
        tabela.setAttribute('data-tablesaw-mode', 'stack');
    } else {
        // Remove o atributo
        tabela.removeAttribute('data-tablesaw-mode');
    }
}

then place the document to listen to the event:

// Escuta o evento
window.addEventListener('resize', screen_width);

call the function to check when the page is loaded.

// Executa a primeira vez
screen_width();

Complete code:

var tabela = document.getElementById('table-hide');

function screen_width() {
    var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    if(width < 400) {
        // Adiciona o atributo
        tabela.setAttribute('data-tablesaw-mode', 'stack');
    } else {
        // Remove o atributo
        tabela.removeAttribute('data-tablesaw-mode');
    }
}

// Escuta o evento
window.addEventListener('resize', screen_width);

// Executa a primeira vez
screen_width();

References:

  • Okay friend, thank you very much! The only problem is that now it is inserting and removing but it is not doing what it should do when they have this data-tablesawmode I will try to solve already helped me enough!

  • I think if I’ve been using some script, you should call it again.

  • I already realized my mistake, the jquery scritps was blocking the plugin I’m using! Working perfectly!!

1

Try to do so

var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; 

if(width < 39.9375){ //Valor em pixeis
   document.getElementById('table-hide').setAttribute("data-tablesaw-mode", "stack");
}
  • Friend does not seem to be working! At least it is not inserted and I have already converted the in pixels!

  • Put this on and say what you return alert(width)

  • Correctly returns the current width value! I tested in 3 different page sizes!

  • I had written getElementsById and must be getElementById, try now

  • Result is the same as before not inserting on the site!

  • I took a test on jsfiddle and gave correctly

  • I’m inserting it just like the one you put in

  • Some error in the console?

  • No, it shows nothing!

  • I’ll give you a point for trying to help just needed what the friend gave down!

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.