0

I have a page with h1 and h2 where I would like to make h1's content dynamically change depending on the value of a specific cookie. I have created a cookie which stores test results of the user (ranging 1-9) and I would like to set a different h1 for each number.

The code that I have is the following but it doesn't seem to work :

function CTAtext () {
    var cookie = getCookie("resultNum");
    var text1 = document.getElementsByClassName("item-h1")[0].innerHTML;
    var text2 = document.getElementsByClassName("copy-text")[0].innerHTML; 
    
  if (cookie == null) {
    text1 = Green; }}
 
  • `var text1 = document.getElementsByClassName("item-h1")[0].innerHTML;` is the same as `var text1 = "Some string";`. There is no connection to any Element. There is no such thing as mutable strings or pointers to primitives in JS. – Sebastian Simon Apr 04 '22 at 07:42
  • So what a solution would look like? – javanovice123 Apr 04 '22 at 07:44
  • `const yourHeader = document.getElementsByClassName("item-h1")[0];`…`yourHeader.innerHTML = Green;`. Just like in the linked Q&A. – Sebastian Simon Apr 04 '22 at 07:46

0 Answers0