Solved

jquery/javascript if statement

Posted on 2011-09-05
11
328 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
 
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

943 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

6 Experts available now in Live!

Get 1:1 Help Now