show a cell when a condition is true

I'm having this table:

<table>
            <tr>
                <script>
                var estimate_type = {{fields.quote_version}};
                var latest_version = {{fields.is_latest_version}};

                if (latest_version === "Y")
                    $(document).ready(function () {
                    $('.initial').show();
                });

                </script>
                                      <td>Estimate type</td><td class="initial">Initial</td>
                                      <td class="modified">Modified estimate #</td>
                                      <td class="cancelled">Cancellation and replacement of estimate #</td>

            </tr>
        </table>

Open in new window


class "initial" is hidden by default.
var latest_version get a "Y" or "N", the field is the database is NAVCHAR type.
I am trying to have this cell (<td>Estimate type</td><td class="initial">Initial</td>) appear when the content of the var is Y.
It's not working.

Thank you.
Alain BensimonAsked:
Who is Participating?
 
Alain BensimonAuthor Commented:
I just found the solution. since {{is_latest_version}} returns a character and not a numeric value, I had to declare it like this in my script, with the double quotes.
now it works :)

var latest_version = "{{is_latest_version}}";

anyway, thank you for your help.
0
 
Jan LouwerensSoftware EngineerCommented:
<table>
   <tr>
     <script>
     
      var estimate_type = {{fields.quote_version}};
      var latest_version = {{fields.is_latest_version}};
      
      
      $(function() {
         if (latest_version === "Y")
            $('.initial').show();
      });

      </script>
      <td>Estimate type</td><td class="initial" style="display: none;">Initial</td>
      <td class="modified">Modified estimate #</td>
      <td class="cancelled">Cancellation and replacement of estimate #</td>

   </tr>
</table>

Open in new window

0
 
Alain BensimonAuthor Commented:
Thank you.
But actually, why do you put a dis^lay: none?
the cell is already hidden by a jquery script above.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Jan LouwerensSoftware EngineerCommented:
Oh, that should be removed. Sorry, that was just left over from when I was trying out the code for myself. (I cheated, and didn't use css.)
0
 
Alain BensimonAuthor Commented:
I will try that tomorrow, when I'll be at work but I'm not sure what the problem is. It might be a var type problem, because when I do this kind of test, it works.

if (estimate_type = 1)
                    $(document).ready(function () {
                    $('.initial').show();
                });
My problem is I might have several revision of quotes, and I Have this field that returns Y if it's the latest version and N if not.
But for some reason, it's not working on that one.
Maybe my var "latest_version" does not get the value the proper way?
0
 
Jan LouwerensSoftware EngineerCommented:
I believe the problem is that you're trying to declare an onReady function within a conditional. I don't think that's going to work. You need to declare the onReady function at the global level, then have the conditional run inside the onReady function.
0
 
Alain BensimonAuthor Commented:
I'll try your solution tomorrow and will let you know.
0
 
Chris StanyonCommented:
There's nothing wrong with your code as it is, so like you say, it's probably a value issue. Dump the value to console to check it:

var latest_version = {{fields.is_latest_version}};
console.log(latest_version);
if (latest_version === "Y")

Open in new window

You can view the console by pressing F12 in your browser.

And bear in mind, your code will only work if you have 1 .initial class. If you have more than one, then you will show them all based on the single if statement.
0
 
Alain BensimonAuthor Commented:
The problem is that {{fields.is_latest_version}} comes from a database and I won't be able to access it from a console.
It's a little bit special, let me explain.
I build some reports using html, and the data are fetched from a sql database and the program generates a PDF report like the one I've uploaded here.
But Ican't test the values in console.
The only thing I know, is that if I make a cell and with "{{fields.is_latest_version}}", I get a result in my report (Y or N), and that is the value I'd like to test so i can show or hide .initial
test_estimate.pdf
0
 
Chris StanyonCommented:
You're not accessing the fields.is_latest_version. You're outputting the latest_version variable.

... and if you can't output that to the console, then it's clearly not set in your Javascript, so your whole approach to the function will fail. If the data is available in your HTML, then it's available to the Javascript.

Have your tried console.log(latest_version)
0
 
Alain BensimonAuthor Commented:
The data is available in my HTML, but only at the moment that I generate the report.
When I do that, a SQL query is launched, returns the result and fill up all fields.

I know one thing though.
If I change my script with the following one, the script works.
If quote_version is one, the .initial is displayed, and if it is more that 1, its stays hidden, which proves that it gets the value.
I think that for some reason it only works when it's a numeric value, and the other field is a character.

<table>
            <tr>
                <script>
                var estimate_type = {{fields.quote_version}};
                var latest_version = {{fields.is_latest_version}};

                if (lf estimate_type= 1)
                    $(document).ready(function () {
                    $('.initial').show();
                });

                </script>
                                      <td>Estimate type</td><td class="initial">Initial</td>
                                      <td class="modified">Modified estimate #</td>
                                      <td class="cancelled">Cancellation and replacement of estimate #</td>

            </tr>
        </table>
0
 
Chris StanyonCommented:
OK. Exactly my point - your Javascript can see the value that comes from {{fields.quote_version}} because it's available at that time. So it stands to reason that it can also see {{fields.is_latest_version}} at that time. Therefore, you're assigning some value to the latest_version variable. In order to debug your code, you need to output that variable to the console, so you can see whether it contains "Y" or something else.

Add the console.log line in and let us know what it says.

It could simply be that the latest_version is a boolean (true/false, 0/1) rather than a Y/N, but until you log the value to the console you won't know.
0
 
Alain BensimonAuthor Commented:
I tried the console, and I get a syntax error.
it does not accept the {{}} of my "{{fields.quote_version}}".
0
 
Chris StanyonCommented:
OK. You're NOT outputting {{fields.quote_version}}. In your code, you assign that value to a variable called latest_version. It's that variable that you should be outputting to the console. If that's causing you an error, then your whole code will fail and you can't do what you're hoping to do. At worst, you should just get undefined sent to the console.

Do you have a page link where we can see this in action.
0
 
Alain BensimonAuthor Commented:
I found the way to make it work
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.