0

I have been reading and studying about this library to be able to change the code of this codebox from jquery to javascript (https://codepen.io/mrWilson123/pen/OJMVwzd?editors=1010) but I got stuck in this part.

$(".slide-captions").html(function () {
        return (
          "<h2 class='current-title'>" +
          currentTitle +
          "</h2>" +
          "<h3 class='current-subtitle'>" +
          currentSubtitle +
          "</h3>"
        );
      });

I understand that it's getting the div element and putting inner html code into it so I tried this but it didn't work.

document.querySelector(".slide-captions").innerHTML(
          "<h2 class='current-title'>" +
            currentTitle +
            "</h2>" +
            "<h3 class='current-subtitle'>" +
            currentSubtitle +
            "</h3>");

What am I doing wrong if someone could help me, thanks.

4
  • 4
    .innerHTML = "..." (it's a property, not a function). Please always check the API docs first, especially before posting a question. Commented Jun 7, 2022 at 18:45
  • 2
    Did you look up what innerHTML is and how to use it? Commented Jun 7, 2022 at 18:46
  • 2
    Did you open up your browser's developer tools (F12) and look at the Console? If so, you would see an error message there telling you that innerHTML is not a function. Commented Jun 7, 2022 at 18:47
  • As I said, I studied jquery for a while to be able to make the change and I ended up getting confused, I forgot the = , what an idiotic mistake hahaha, I should delete this question hahaha, thanks anyway. Commented Jun 7, 2022 at 18:51

2 Answers 2

1

Whenever appending to the DOM, there is a string-oriented approach using parse-able strings of HTML and properties and methods like:

  • .innerHTML
  • .insertAdjacentHTML()

There is also a construction-oriented approach using DOM Nodes and properties and methods like:

  • .insertBefore() / .before()
  • .appendChild / .append()
  • .insertAdjacentElement()
  • .classList

Whichever approach you normally use to build DOM, it's useful to be aware of the other approach:

String-oriented Approach

let slideCaptions = document.querySelector('.slide-captions');

let myHTML = '';
myHTML += '<h2 class="current-title">' + currentTitle + '</h2>';
myHTML += '<h3 class='current-subtitle'>' + currentSubtitle + '</h3>';

slideCaptions.innerHTML = myHTML;

Construction-oriented Approach

let slideCaptions = document.querySelector('.slide-captions');

let titleElement = document.createElement('h2');
titleElement.classList.add('current-title');
titleElement.textContent = currentTitle;
slideCaptions.appendChild(titleElement);

let subtitleElement = document.createElement('h3');
subtitleElement.classList.add('current-subtitle');
subtitleElement.textContent = currentSubtitle;
slideCaptions.appendChild(subtitleElement);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

document.querySelector('.slide-captions').innerHTML=`
        <h2 class='current-title'>" +
        currentTitle +
        "</h2>" +
        "<h3 class='current-subtitle'>" +
        currentSubtitle +
        "</h3>`;

if there any error use this instead

document.querySelectorAll('.slide-captions')[0] // .innerHTML ....

2 Comments

Using template literals is meant to replace fumbling around with + and quotes. Use either, not both.
I did what was asked but thanks anyway

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.