Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a change functions as below: All i want is whan a user clicks the button and the value is less than zero change the backgoundcolor of the textbox and viceversa if its greater that. But it doesn't seem to work any ideas?

 $("input[name$=Text1]").change(function () {
     var value = $(this).val();
      $("p#pcms").text(value);

       var PerMonth = value-Pmmc;
        $("input[name$='spmtext']").val(PerMonth);

        if (PerMonth <0){
        $("input[name$='spmtext']").css("background-color","#FFFF00");
        }

         if (PerMonth >0){
        $("input[name$='spmtext']").css("background-color","#004080");
        }


}).change();
share|improve this question
add comment

closed as off topic by Alex Angas Aug 22 '10 at 23:30

Questions on SharePoint Stack Exchange are expected to relate to SharePoint within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

up vote 0 down vote accepted

As Brian said, you've got a couple issues here. I personally wouldn't hard code the selector on all of the CSS lines either. Since what you're referencing there is the selector, you can just use $(this), which will make your code a lot cleaner.

ie:

if (PerMonth <0){
  $(this).css("background-color","#FFFF00");
}
share|improve this answer
    
Thats of the the this now i know how to pay better with the selectors. About if i want to show images instead of a backgound color? –  Patrick Aug 23 '10 at 7:17
    
You'd use the same method, just different CSS. For example, $(this).css("background", "#00ff00 url('smiley.gif') no-repeat fixed center;"); –  webdes03 Aug 23 '10 at 13:17
add comment

I see a couple of things wrong with your script,

  1. you're binding to the change event for the selector. not the click event for a button, maybe your question is phrased.

  2. where are you defining the variable reference for Pmmc ?

Also, im not sure what the secondary purpose of calling the change() at the end is for.

share|improve this answer
    
Thanks Brian Quite new to Jquery but will catch up soon. The code did eventually work but i has to check the initial value of the text box before i actually change the color. The scenario is quite complicated as i have different webparts that loads on the page and when it load the css apply on the fly. So all i did what check for the value of the textbox before i applied the CSS. N –  Patrick Aug 23 '10 at 7:16
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.