0

I need to change the CSS class name of all the elements in a page with a particular class name (.k-textbox). I tried the below code but it does not hit inside the .each() function

<script>
    $(document).ready(function () {    
        $(".k-textbox").each(function () {
            //alert("a");
            $(this).removeClass("k-textbox");
            $(this).addClass("input-medium");
        });
    });
    </script> 

In the page i have a 3rd party grid control. the CSS class i have mentioned is inside that third party grid control.

below is the DOM object:

enter image description here

6
  • whether the input elements are created dynamically Commented Oct 23, 2013 at 10:16
  • Show us your html? maybe you have the class wrong or something Commented Oct 23, 2013 at 10:17
  • Although you don't need the each, this should work. Please add your HTML and any other JS code to the question so we can see what's happening. Commented Oct 23, 2013 at 10:18
  • Are those elements in the DOM created after the page has been loaded? Commented Oct 23, 2013 at 10:25
  • @RoryMcCrossan yes it's created dynamically Commented Oct 23, 2013 at 10:29

2 Answers 2

6

You should try to use chaining API provided by jQuery library:

$(".k-textbox").removeClass("k-textbox").addClass("input-medium");

edit:

As long as the elements are created dynamically you could try to run this code after those elements are created. But if you don't know when they are inserted into the code and doesn't have control over them you could try write simple watch function, i.e:

var watchTimer = setInterval(function () {
      var inputs = $('.k-textbox');
      if (inputs.length) {
          // clear interval
          clearInterval(watchTimer);

          // change class
          inputs.removeClass("k-textbox").addClass("input-medium");
      }
    }, 100);
Sign up to request clarification or add additional context in comments.

3 Comments

While this is better syntax, logically it's no different to what the OP has. I'm not sure why it's being upvoted?
@chamara - does that inputs created dynamically after DOM is ready? And can you know when it's happen?
Do you have control over that when they are inserted? (click some button, call some function, etc...)
-1

use addClass() and removeClass()

$(".k-textbox").removeClass("k-textbox").addClass("input-medium");

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.