Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

change h2 text with javascript

I want to change my h2 text to something else when i write in the input field and hit the button.

          <label>Title
              <input id="titleTxt" type="text">
          </label>
          <input type="submit" id="titleBtn" value="Add title" onclick="getResault()">
          <br>
          <br>
          <label>Add new idea
              <input id="newIdea" type="text">
          </label>
          <input type="submit" id="byBtn" value="Add" onclick="getResault()">

          <h2 id="ideTitle">What to do on saturday</h2>
(function(){

    //HTML objects
    var titleTxt;
    var titleBtn;
    var ideTitle;


    var init = function(){

        var setHTMLObjects = function(){
            titleTxt = document.getElementById("titleTxt");
            titleBtn = document.getElementById("titleBtn");
            ideTitle = document.getElementById("ideTitle");
        }();//end setHTMLObjects

        var setEvents = function(){
            titleBtn.onclick = saveTittel;
        }();//end setEvents

    }();//end init

    function saveTittel(){
    ideTitle.innerHTML = titleTxt.value;
    }

Hi Anders, I've edited your markup so that your code will display properly. Check out the markdown cheatsheet at the bottom of this page to see how.

1 Answer

Steven Parker
Steven Parker
243,318 Points

:point_right: It looks like you're missing the closing of the outer function, and its invocation.

Unless Justin accidentally truncated it while he reformatted. Try adding this at the bottom:

})();