Solved

jquery/javascript if statement

Posted on 2011-09-05
11
334 Views
Last Modified: 2012-05-12
Hi, I've been trying unsuccessfully to add another if statement that would only allow the background color to be applied if a field "Frequency"="7 Days" otherwise no formatting would be applied.

<script type="text/javascript">
if(typeof jQuery=="undefined"){
var jQPath="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/";
document.write("<script src='",jQPath,"jquery.min.js' type='text/javascript'><\/script>");
}
</script>
<script>



$(document).ready(function(){

    var today = new Date();
    $(".ms-vb2:eq(2)", "tr").each(function() {
        var p = $(this).text().split(" ");
        var d = p[0].split("/");
        var h = p[1].split(":");
        if(d.length == 3 && h.length == 2) {
            var Modified = new Date(d[2],d[1]*1-1,d[0], h[0],h[1],0);
            if(today-Modified<(1*86400*1000)) {
                $(this).parents("tr:eq(0)").css("background-color", "#90EE90");

            }
       else if(today-Modified<(1*172800*1000)) {
                $(this).parents("tr:eq(0)").css("background-color", "#F0E68C");

            }

            else {
                $(this).parents("tr:eq(0)").css("background-color", "#FFC7CE");

            }

        }
    })
});

</script> 




 

Open in new window

0
Comment
Question by:dgoldie
11 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36484262
== instead =
0
 
LVL 17

Assisted Solution

by:OmniUnlimited
OmniUnlimited earned 250 total points
ID: 36484283
When you say "field", are you referring to an input field?  Is the field present between the <td></td>'s?
Is "Frequency" the name or the id?

Assuming that you are referring to an input field with an id="Frequency" and a value of "7 Days", your code would be like this:
<script type="text/javascript">
if(typeof jQuery=="undefined"){
var jQPath="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/";
document.write("<script src='",jQPath,"jquery.min.js' type='text/javascript'><\/script>");
}
</script>
<script>



$(document).ready(function(){

    var today = new Date();
    $(".ms-vb2:eq(2)", "tr").each(function() {
        var p = $(this).text().split(" ");
        var d = p[0].split("/");
        var h = p[1].split(":");
        if(d.length == 3 && h.length == 2) {
            var Modified = new Date(d[2],d[1]*1-1,d[0], h[0],h[1],0);
            if(today-Modified<(1*86400*1000)) {
                if ($(this).find("input#Frequency").val() = "7 Days")
                     $(this).parents("tr:eq(0)").css("background-color", "#90EE90");

            }
       else if(today-Modified<(1*172800*1000)) {
                if ($(this).find("input#Frequency").val() = "7 Days")
                     $(this).parents("tr:eq(0)").css("background-color", "#F0E68C");

            }

            else {
                if ($(this).find("input#Frequency").val() = "7 Days")
                     $(this).parents("tr:eq(0)").css("background-color", "#FFC7CE");

            }

        }
    })
});

</script>

Open in new window

0
 

Author Comment

by:dgoldie
ID: 36484358
Hi, its actually the sharepoint collumn name so if I was trying to highlight the row without the time comparrison I'd use something like this
if ($Text = $("td.ms-vb2:contains('7 Days')")

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 11

Expert Comment

by:Kusala Wijayasena
ID: 36484936
It is great if you can pate HTML output of your table. then we can give your better solution

-Kusala
0
 

Author Comment

by:dgoldie
ID: 36487398
I've had to x out a lot of info but this should give you what you need.

 Test.txt
0
 
LVL 3

Expert Comment

by:abhisheksimion
ID: 36547310
if ($Text = $("td.ms-vb2:contains('7 Days')"))

Open in new window

should be
if ($Text == $("td.ms-vb2:contains('7 Days')"))

Open in new window

0
 

Author Comment

by:dgoldie
ID: 36548407
thanks, I,ll have to look at it on the weekend due to heavy work load
0
 

Accepted Solution

by:
dgoldie earned 0 total points
ID: 36931641
After some time away I was able to revisit the problem and manged to find a solution using the code leakim971 helped me with. The code below works using the IndexOf method but I guess there is probable an easier way of getting there.

I'll Accept and Award point shortly but I'll wait a bit in case anyone wants to add to the solution.

Many Thanks

<script type="text/javascript">
if(typeof jQuery=="undefined"){
var jQPath="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/";
document.write("<script src='",jQPath,"jquery.min.js' type='text/javascript'><\/script>");
}
</script>
<script>

$(document).ready(function(){

    var today = new Date();
    $(".ms-vb2:eq(0)", "tr").each(function() {
        var p = $(this).text().split(" ");
        var d = p[0].split("/");
        var h = p[1].split(":");
        if(d.length == 3 && h.length == 2) {
            var Modified = new Date(d[2],d[1]*1-1,d[0], h[0],h[1],0);
            if(today-Modified>(1*240*1000)){				

		$(this).parents("tr:eq(0)").css("background-color", "red");		
		
            }

else if(today-Modified>(1*120*1000) && today-Modified<(1*240*1000)){

		var me = $(this).parents("tr:eq(0)").text().indexOf("14 Days")

		if (me == -1) {

		$(this).parents("tr:eq(0)").css("background-color", "Red");

		}

		else if (me != -1){
		
		$(this).parents("tr:eq(0)").css("background-color", "orange");

		} 			            		
		

            }

else if(today-Modified>(1*60*1000) && today-Modified<(1*120*1000)){



		$(this).parents("tr:eq(0)").css("background-color", "orange");                               	
		

            }

else if(today-Modified<(1*60*1000)) {

                $(this).parents("tr:eq(0)").css("background-color", "green");		

            }

        }
    })
		
		$Text =$("td.ms-vb2:contains('Pass')");
		$Text.css("color", "green");
		$Text.css("font-weight", "bold");
		$Text =$("td.ms-vb2:contains('Fail')");
		$Text.css("color", "white");
		$Text.css("font-weight", "bold");

});

</script>  

Open in new window

0
 

Author Comment

by:dgoldie
ID: 36940985
I've requested that this question be closed as follows:

Accepted answer: 0 points for dgoldie's comment http:/Q_27291754.html#36931641
Assisted answer: 0 points for dgoldie's comment http:/Q_27291754.html#36548407

for the following reason:

The code from a previous question helped me resolve this issue but I find it difficult to ward a high grade or points because an actual solution wasn’t provided by the experts although their responses did force me to find a solution by myself.
0
 

Author Comment

by:dgoldie
ID: 36940986
please cancel this
0
 

Author Closing Comment

by:dgoldie
ID: 36967143
The code from a previous question helped me resolve this issue but I find it difficult to ward a high grade or points because an actual solution wasn’t provided by the experts although their responses did force me to find a solution by myself.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now