Move class name from child to parent

Looking to move the class name of an image to it’s containing figure element using jQuery…

<figure class="wp-caption">
    <img class="imghvr-hinge-up" src="image.jpg">
    <figcaption >A sample caption</figcaption>
</figure>

In particular I would like to move any img tag class name beginning with ‘imghvr-‘ to it’s containing figure, if that containing figure has the class name ‘wp-caption’. The result would look like the following…

Read More
<figure class="wp-caption imghvr-hinge-up">
    <img class="" src="image.jpg">
    <figcaption >A sample caption</figcaption>
</figure>

Hope that makes sense!) Any help would be greatly appreciated!

Related posts

4 comments

  1. You could do it like this

    $('img[class^="imghvr-"]').each(function() {
      var cl = $(this).attr('class');
      $(this).removeClass().parent('[class="wp-caption"]').addClass(cl);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <figure class="wp-caption">
      <img class="imghvr-hinge-up" src="image.jpg">
      <figcaption>A sample caption</figcaption>
    </figure>
  2. $( 'img[class^="imghvr-"]' ).each( function() {
      var imghvrClass =  $(this).attr('class').match(/imghvr-.*/)[0]
      var $parent = $(this).parent()
    
      if ( $parent.hasClass( 'wp-caption' ) ) {
        $(this).removeClass( imghvrClass )
        $parent.addClass( imghvrClass )  
      }
    })
    
    
    <figure class="wp-caption">
        <img class="imghvr-hinge-up" src="image.jpg">
        <figcaption >A sample caption</figcaption>
    </figure>
    <figure class="wp-caption">
        <img class="imghvr-hinge-up2" src="image.jpg">
        <figcaption >A sample caption</figcaption>
    </figure>
    
  3. var imgs = document.querySelectorAll(".wp-caption img");
    
        for(var i=0; i<imgs.length; i++) {
            var img = imgs[i];
            if(img.className.startWith("imghvr-")) {
                img.parentChild.classList.push(img.className);
                img.className = '';
            }
    
        }
    

    Something like that. I didn’t test.

    Edit: As always there is smarter than me ^^

  4. You can utilize .closest(), .addClass(function(){}), .is()

    var c = "[class^=imghvr-]";
    
    $("img" + c)
      .closest("figure")
      .addClass(function() {
        if ($(this).is(".wp-caption")) {
          var img = $(c, this);
          var imgClass = img[0].className;
          img[0].className = "";
          return imgClass;
        }
      })
    img[class^="imghvr-"] {
      border: 1px solid blue;
    }
    figure[class*="imghvr-"] {
      border: 1px solid red;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
    </script>
    <figure class="wp-caption">
      <img class="imghvr-hinge-up" src="image.jpg">
      <figcaption>A sample caption</figcaption>
    </figure>
    
    <figure class="wp-caption">
      <img class="imghvr-hinge-up" src="image.jpg">
      <figcaption>A sample caption</figcaption>
    </figure>
    
    <figure class="wp">
      <img class="imghvr-hinge-up" src="image.jpg">
      <figcaption>A sample caption</figcaption>
    </figure>

    Alternatively, using .toggleClass(), .is(), .find(), .attr()

    var c = "[class^=imghvr-]";
    
    $("figure.wp-caption").toggleClass(function() {
      var img = $("img", this);
      return img.is(c) ? img.attr("class") : false
    }).find(c).attr("class", "");
    img[class^="imghvr-"] {
      border: 1px solid blue;
    }
    figure[class*="imghvr-"] {
      border: 1px solid red;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
    </script>
    <figure class="wp-caption">
      <img class="imghvr-hinge-up" src="image.jpg">
      <figcaption>A sample caption</figcaption>
    </figure>
    
    <figure class="wp-caption">
      <img class="imghvr-hinge-up" src="image.jpg">
      <figcaption>A sample caption</figcaption>
    </figure>
    
    <figure class="wp">
      <img class="imghvr-hinge-up" src="image.jpg">
      <figcaption>A sample caption</figcaption>
    </figure>

Comments are closed.