0

Here is my JQuery Code and I would like to compare the text in the text variable "text" to the possible CR Status (e.g. Approved Implemented), but I don't know how to do it.

_spBodyOnLoadFunctionNames.push("colouring");

function colouring(){
    var $th = $("div[Name='CR_x0020_Status']").parent().css("background-color","#66CD00");
    var $index = $th.index();
    $th.parent().parent().children(".ms-itmhover").each(function(index, elem) {
        var $div = $($(this).children("td").get($index));
        var text = $div.text();
        $("text:contains('Approved')").css("background-color","#66CD00");
    })
}

I tried different options, but I still don't know how to do it!

Best regards and thank you in advance
Matthias

Harsh Baid
  • 7,199
  • 5
  • 48
  • 92
user24555
  • 39
  • 1
  • 1
  • 8

2 Answers2

1

You can't put variables in jQuery selectors. $("text:contains(Approved)") means an element with tag text that contains Approved. You could write:

if (text.indexOf('Approved') > -1)

But you don't need the .each() loop at all, you can do it all with jQuery selectors:

$th.parent().parent().find(".ms-itmhover > td:contains(Approved)").css("background-color", "#66CD00");
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • "You can't put variables in jQuery selectors." -- not sure this is relavent to the question, but you can. e.g.: http://stackoverflow.com/questions/5891840/how-to-use-javascript-variables-in-jquery-selectors – graphicdivine Jun 03 '14 at 08:29
  • That's not the same thing. That's using a variable to provide the ID in the selector, it's not searching the contents of a variable as the OP tried. – Barmar Jun 03 '14 at 08:31
  • Oh.. I see what you mean. Hadn't quite grokked it. – graphicdivine Jun 03 '14 at 08:43
0

Hard to tell exactly what you need, but possibly:

if(text == "Approved Implemented"){
    ...do more stuff here...
}

Possibly, you just need to duplicate the existing line and modify it for another colour:

$("text:contains('Approved')").css("background-color","#66CD00");
$("text:contains('Approved Implemented')").css("background-color","#0066CD");
graphicdivine
  • 10,937
  • 7
  • 33
  • 59