• Resolved wdgrimm

    (@wdgrimm)


    Hello dear wordpress cracks,

    I want to add a css class to a span element, that has certain content.
    There are 3 stati where I want to add classes:

    <span class="statusfield">Done</span>
    <span class="statusfield">In Progress</span>
    <span class="statusfield">Pending</span>

    Based on the content of the span element, a certain class should be added to the element.
    I have now tried the following code using the plugin header “Footer Code Manager Pro”:

    <script>
      var status = {
    
    	   "Done": "done",
    	   "In Progess": "in-progress",
    	   "Pending": "pending"
    	}
    
      $(".statusfield").each(function() {
    $(this).addClass(status[$(this).html()]);
    
    });
    		 
    </script>

    Unfortunately the code does not work. I have tried everything. Include in the header and in the footer with “document ready” and without.
    Slowly I am a little desperate.

    Does anyone of you have a tip how I can solve this?

    That would be great!
    Have many thanks in advance!

    greetings
    Bernd

    • This topic was modified 1 year, 8 months ago by wdgrimm.
    • This topic was modified 1 year, 8 months ago by wdgrimm.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Try this in your footer:

    $(document).ready(function() {
      var status = {
        "Done": "done",
        "In Progress": "in-progress",
        "Pending": "pending"
      };
    
      $(".statusfield").each(function() {
        $(this).addClass(status[$(this).html()]);
      });
    });

    IF that doesn’t work, try this which will avoid any confilts with scripts that may be using “$”

     jQuery(document).ready(function($) {
      var status = {
        "Done": "done",
        "In Progress": "in-progress",
        "Pending": "pending"
      };
    
      $(".statusfield").each(function() {
        $(this).addClass(status[$(this).html()]);
      });
    });
    Thread Starter wdgrimm

    (@wdgrimm)

    hi pichichi,

    Thanks for your quick help. i will try it out….

    • This reply was modified 1 year, 8 months ago by wdgrimm.
    Thread Starter wdgrimm

    (@wdgrimm)

    hi pichichi,

    Thanks again for your quick help.
    Version 2 has worked. Great!
    Have a nice day!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘JQuery – Snippet in WP 6.1.1’ is closed to new replies.