SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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();

       var PerMonth = value-Pmmc;

        if (PerMonth <0){

         if (PerMonth >0){

share|improve this question

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.

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.


if (PerMonth <0){
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

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

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