How to check if span has value even after the website refreshes

user3771102

Hi I would like to ask if it is possible to maintain the changes from a function after the whole website refreshes. So far I found a code for changing background color of a span who has a value. But then since it is onclick, the background color changes back to original as the page refreshes.

$('#submit').click(function(){
   $('td span').each(function(index){
       if( $(this).text().length !== 0 ){
          $(this).parent().css('background-color', 'yellow');
       }
   });       
});

JSFIDDLE 1

Is there a way to maintain the changes even after the page refreshes or is there another way to do it? A listener maybe or a checker in which the site will be checked first whether the span has value or not. Also instead of making the parent's background color change I want the column on the opposite to change. The one without a value. Will it be possible? Javascript or Jquery is okay.

EDIT

Uhmm I guess my question was a bit unclear. Well I edited my code above. You can see there the table in which there are 2 columns. I want it to retain that way after the page refreshes. But so far I get stuck with modifying the code.. and it doesn't save the color anymore. I think that I made a mistake along the way but I cannot figure it out.. :(

JSFIDDLE 2

UPDATE

Okay another try at @trincot's answer, I kinda found what I was looking for but not quite. Its a bit long and its unnecessary to highlight the text and for inputting text. The whole box should be painted not highlighted. Please refer to this JSFIDDLE 1. Below is the one I modified.

JSFIDDLE 3

lost_in_magento

As PartyPete25 suggested, We can make use of HTML5 localstorage.

Here's working JSFiddle

So here is the code for you,

    <table border="1px" width="50%">
  <tr>
    <td><span class="bgValue"></span></td>
    <td><span class="bgColor">1</span></td>
  </tr>
  <tr>
    <td><span class="bgValue"></span></td>
    <td><span class="bgColor">5</span></td>
  </tr>
  <tr>
    <td><span class="bgValue"></span></td>
    <td><span class="bgColor">8</span></td>
  </tr>
</table>
<input type="button" value="Try it" />

JS

var tdValue = $('.bgValue');

 var storedBgColor = localStorage.getItem('.bgColor');

 if (storedBgColor) {
   tdValue.parent().css('background-color', storedBgColor);
 } else {
   // set default color
   alert('no color set');

 }


 $('input').click(function() {

   $('td span').each(function(index) {

     if ($(this).text().length !== 0) {
       $(this).parent().css('background-color', 'yellow');
       var bgColor = $('.bgColor');

       localStorage.setItem('.bgColor', 'yellow');


       // When a span with some value is found, alert the index of the row
       alert($(this).closest('tr')[0].rowIndex);

     }

   });
 });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to check if the register has a even value?

How to check if any span element in inside div has empty value

How to check if value has even parity of bits or odd?

Check if span contains value but value is after another nestled span

How to check if <InputB /> has a value after a value for <InputA /> has been selected?

How to keep input field placeholder value even after its value is typed in (field has no label)

How to retain multiple checkbox even page refreshes reset after form submit

how to check if element has this value or not

How to check if an element in Pandas df row has a value with equality sign(the value after the sign changes)

how to check if std::span has the required layout at compile time?

Page refreshes after the dropdown value is selected in React

How to check if an arrayList has a value inside of a value

Array element has 0 value even after assigning it

How to check if an array has the value one after another in other array PHP?

How to check if website post title has certain word in it?

How to check if website has http/2 protocol support

How to check if a website has HTTP/2 protocol support

Bash: How can I check if a website has finished loading?

How to check a number inside a span and change its color based on the value?

How to check if a value isn't changed in between <span> tag

How to keep value in variable between page refreshes?

PHP How to check if array has not key and value

How to check the table has value using Jquery

How to check if a double value has no decimal part

How to check if SQL column has value?

How to check if an array has a value multiple times

How to check whether a float has an integer value

How to check if EditText has a value in Android / Java

How to check if an <li> element has a specific value