1

I used the above code to apply a background color on a line with the word High in a field (choice). It does not work. I have used a code that matches exactly the word as in other fields (descriptions etc) there might be written the word High in other contexts.

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>

<script type="text/javascript">
    $(document).ready(function(){ 

    $("td .ms-vb2").filter( function (index) {

       return $(this).text() == "High";

    })
$Text.parent().css("background-color", "#F5A9A9");

});
</script>

What am I doing wrong?

3
  • Is it related to SharePoint? if not, it should be asked here: stackoverflow.com Commented Jul 3, 2017 at 11:43
  • SharePoint version? Commented Jul 3, 2017 at 17:17
  • online........... Commented Jul 5, 2017 at 7:50

1 Answer 1

0

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

Instead you can try this (Applies to classic List Views only):

<script type="text/javascript">
    $(document).ready(function(){ 

    $("td.ms-vb2").each( function () {

       if($(this).text() == "High"){
       $(this).parent("tr").css("background-color", "#F5A9A9");
       //$(this).closest("tr").css("background-color", "#F5A9A9");
       }      


    });


});
</script>
2
  • I was able to use your code with 4 different words and 4 different colors. Am I right in thinking that because you have put '==' (strictly equal to the word in the string) it means that if there is a comment field in the table and by chance there is a sentence that includes the world 'High' (among other words) it will not trigger the color change? Commented Jul 14, 2017 at 10:48
  • Correct. The IF condition in the above code is looking for an exact match with in the matched TAG. There is a different way to check the occurrence of a word - check here w3schools.com/jsref/jsref_indexof.asp Commented Jul 14, 2017 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.