?
Solved

jquery/javascript if statement

Posted on 2011-09-05
11
Medium Priority
?
360 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 1000 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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 demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

752 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