1

I'm trying to change the text of a button to that of a value stored in a variable. The button is currently blank and not even using a fixed value like .value = "test"; is working.

HTML:

<div id="addContainer">
    <textarea id="listTitleInput" class="textarea" placeholder="Add the title of your list here, then click 'Add List'." rows="10" cols="50"></textarea>
    <button id="addListBtn" data-role="button">Add List</button>
</div>

<div id="listDisplayContainer">
</div>

JS:

$(document).ready(function () {

//LISTS
    var listTitleInput = document.getElementById("listTitleInput");
    var addListBtn = document.getElementById("addListBtn");
    var listCount = localStorage.getItem("listCount"); 

if (listCount === null) {
        noteCount = 0;
    }

//ADD LISTS
    function addList() {
        if ($("#listTitleInput").val() == "") {
            alert("Please give your list a title, then click 'Add List'.");
        } else {
            listCount++;
            var list = $("#listTitleInput").val();
            console.log("List Count: " + listCount);
            console.log(list);
            var display = document.createElement("button");
            document.getElementById("listDisplayContainer").appendChild(display);
            display.className = "ui-btn";
            display.id = "list" + listCount;
            $("#list" + listCount).value = list;

        }
    }

//Lists
    addListBtn.addEventListener("click", addList);

});
9
  • 2
    <button> elements don't have a "value" property. You have to change the .innerHTML to change the content. Commented Apr 25, 2017 at 21:03
  • 2
    Also I get confused a little when I see a mix of jQuery and native DOM code. Why not stick with one or the other? Commented Apr 25, 2017 at 21:05
  • Unfortunatley $("#list" + listCount).innerHTML = list; isn't working either Commented Apr 25, 2017 at 21:07
  • 1
    Thats because innerHTML is a native DOM atrtribute. You need to use .html() Commented Apr 25, 2017 at 21:08
  • 1
    This kind of thing is why mixing DOM stuff and jQuery stuff always makes my head spin. Go with one or the other until you're really comfortable with the relationship. Commented Apr 25, 2017 at 21:09

2 Answers 2

2

Looks like you need to change $("#list" + listCount).value = list; to $("#list" + listCount).text(list);

value is not a property and val() doesn't work for a button.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that you are confusing native DOM attributes with jQuery ones.

$("#list" + listCount).value = list;

$("#list" + listCount) is a jQuery object so it doesn't use the native javascript properties that you may be used to. (value=)

What you are looking for is:

$("#list" + listCount).html(list);

Or

$("#list" + listCount).text(list);

Since list is a string value, it will be best to use .text

Comments

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.