2

I want to update my object with values from text fields.

I think the problem is with the click eventhandler on the button but not sure. I've tried a few things, Your help would be amazing.

HTML

<form>
    <label><p>Book Name: </p></label>
    <input name="booktitle" id="booktitle" type="text" value="I'm a value">
    <label><p>Total Pages: </p></label>
    <input type="text">
    <label><p>Current Page: </p></label>
    <input type="text">
    <button id="my-Btn" type="button">Add to List</button>
</form>

JS

(function() {
// Create book object

var book = {

    name: 'JavaScript & jQuery',
    totalPages: 622,
    pages: 162,
    pagesLeft: function() {
        var total = this.totalPages - this.pages;
        return total;
    },
    percentageLeft: function() {
        var totalPercentage = this.pagesLeft() / this.totalPages * 100
        return Math.round(totalPercentage);
    }
};

// write out book name and pages info

var bookName, totalPages, pagesLeft, percentageLeft; //declares variables

bookName = document.getElementById('bookName'); // gets elements from document
totalPages = document.getElementById('totalPages');
pagesLeft = document.getElementById('pagesLeft');
percentageLeft = document.getElementById('percentageLeft');

bookName.textContent = book.name; // write to document
totalPages.textContent = 'Total Pages: ' + book.totalPages;
pagesLeft.textContent = book.pagesLeft();
percentageLeft.textContent = book.percentageLeft() + '%';

// pull value from text field and set to object

document.getElementById("my-Btn").addEventListener("click", function() {
    book.name = document.getElementById('booktitle').value;
});

}());

Code Pen of what I have so far.

http://codepen.io/Middi/pen/pRGOVW

Thanks in advance.

2 Answers 2

2

Your code already updates an object's property (book.name) with a value from a text field (#booktitle). You can see this by adding alert(book.name); after the line

book.name = document.getElementById('booktitle').value;

As Jazcash noted, if you wanted to display the updated book name everytime it was changed, you'd need to add

bookName.textContent = book.name;

In your eventlistener, so it'd look something like this:

document.getElementById("my-Btn").addEventListener("click", function() {
    book.name = document.getElementById('booktitle').value;
    bookName.textContent = book.name;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome man. Thank you. you saved me from pulling my hair out
1

The problem is you're setting your divs textContent based on book here: bookName.textContent = book.name;. But then you need to do it again in your event like so:

book.name = bookName.value;
bookName.textContent = book.name;

You'll need to do this for all your fields

1 Comment

Legendary, Thank you so much

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.