Move class name from child to parent

Looking for moving a class name of an image to it containing a shape 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 class name of the img tag starting with 'imghvr-' to it containing the figure if the contained figure has the class name 'wp-caption'. The result will look like this:

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

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

+4
source share
4 answers

You can 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>
Run codeHide result
+2
source
$( '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>
+2
source
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 = '';
        }

    }

- . .

: ^^

0

.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>
Hide result

, .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>
Hide result
0
source

All Articles