Solved

jquery/javascript if statement

Posted on 2011-09-05
11
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

705 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