0

I have an AJAX function to hide and show fields of a form in HTML. I want to add some CSS in the AJAX function.

<script>
    $(function() {

        $('#advo_other').hide();
        $('#advocate').change(function() {

            var val = $(this).val();

            $('#advo_other').hide();

            switch (val) {
                case 'Other':
                    $('#advocate') // want to add css there......
                    $('#advo_other').show();
                    break; 
            }
        });
    });
</script> 

and the style is below:

<style type="text/css">
    .fade {
        color: #CCC;
        border-color:#CCC;
    }
</style>
2
  • The first step should be to have a look at the jQuery documentation: api.jquery.com/category/css. Commented Sep 8, 2012 at 11:37
  • OMG o_O I don't know why.. but it has nothing to do with ajax.. O_o Commented Sep 8, 2012 at 11:43

3 Answers 3

2

You can add CSS with Jquery to any element like this:

$('#advocate').css('color', '#005DAA');

Alternatively you can add a class with styling rules to the element

$('#advocate').addClass('fade');
Sign up to request clarification or add additional context in comments.

Comments

1

Use the addClass functionnality.

So you will have:

<script>
    $(function() {

        $('#advo_other').hide();
        $('#advocate').change(function() {

            var val = $(this).val();

            $('#advo_other').hide();

            switch (val) {
                case 'Other':
                    $('#advocate').addClass("fade");
                    $('#advo_other').show();
                    break; 
            }
        });
    });
</script> 

Comments

0

Try;

$(function() {
  $('#advo_other').hide();
  $('#advocate').change(function() {
    var val = $(this).val();
    $('#advo_other').hide();
    switch (val){
      case 'Other':
      $('#advocate').addClass("fade");
      $('#advo_other').show();
      break;
    }
  });
});

$('#advocate') is used to select an element with id advocate. You may create classed in your CSS to style elements. Use addClass("class_name") to add some class styling to your element and .removeClass("class_name") to remove class styling.

Comments

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.