horribly_n00bie
horribly_n00bie

Reputation: 79

Change a dom using .replaceWith

I have some code that does almost exactly what I want. It changes all the strong tags to styled h3 tags, which is perfect, but for the life of me I can't figure out what to replace ".click" with to make it run automatically. I tried ".ready" and it gives me an error in my jquery.

$(document).ready(function(){
    $('strong').click(function(){
        $(this).replaceWith($('<h3 style="margin:0px;display:inline;">' + this.innerHTML + '</h3>'))
    })
});

Upvotes: 0

Views: 3413

Answers (4)

David Thomas
David Thomas

Reputation: 253308

I'd suggest just going straight to replaceWith():

// most (if not all) jQuery methods iterate over
// the collection to which they're chained:
$('strong').replaceWith(function () {

  // here 'this' is the individual <strong> element over
  // which the method iterates, and we return the created element:
  return $('<h3 style="margin:0px;display:inline;">' + this.innerHTML + '</h3>');
});

$('strong').replaceWith(function () {
  return $('<h3 style="margin:0px;display:inline;">' + this.innerHTML + '</h3>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>1</strong>
<strong>2</strong>
<strong>3</strong>
<strong>4</strong>

Incidentally, in plain JavaScript, this is also quite easily possible:

// creating a <h2> element:
var heading = document.createElement('h2'),
// initialising an empty variable:
  clone;

// setting the display and margin properties:
heading.style.display = 'inline';
heading.style.margin = '0';

// using Function.prototype.call() to use
// Array.prototype.forEach() on the array-like
// NodeList returned by document.querySelector():
Array.prototype.forEach.call(document.querySelectorAll('strong'), function(bold) {

  // cloning the created <h2> element:
  clone = heading.cloneNode();

  // setting its innerHTML:
  clone.innerHTML = bold.innerHTML;

  // traversing to the parentNode, and
  // using Node.replaceChild() to replace
  // existing <strong> element with the
  // cloned <h2> element:
  bold.parentNode.replaceChild(clone, bold);
});

var heading = document.createElement('h2'),
  clone;

heading.style.display = 'inline';
heading.style.margin = '0';

Array.prototype.forEach.call(document.querySelectorAll('strong'), function(bold) {

  clone = heading.cloneNode();

  clone.innerHTML = bold.innerHTML;

  bold.parentNode.replaceChild(clone, bold);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>1</strong>
<strong>2</strong>
<strong>3</strong>
<strong>4</strong>

References:

Upvotes: 5

Allen Tellez
Allen Tellez

Reputation: 1211

This should do it.

$(document).ready(function(){
    $('strong').each(function(){
        $(this).replaceWith($('<h3 style="margin:0px;display:inline;">' + this.innerHTML + '</h3>'))
    })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>1</strong>
<strong>2</strong>
<strong>3</strong>

Upvotes: 1

Alex McMillan
Alex McMillan

Reputation: 17952

What you are doing is adding a handler to all the <strong> elements on the page that will trigger whenever one of them is clicked. If you want to perform the replace as soon as the document is ready try this:

$(document).on('ready', function () {
    $('strong').each(function () {
        $(this).replaceWith($('<h3 style="margin:0px;display:inline;">' + this.innerHTML + '</h3>'));
    });
});

Upvotes: 1

epascarello
epascarello

Reputation: 207501

You want to use each() so it iterates over all of them.

$('strong').each(function(){ ... });

Upvotes: 2

Related Questions