Solved

jquery/javascript if statement

Posted on 2011-09-05
11
322 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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)
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…

762 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

19 Experts available now in Live!

Get 1:1 Help Now