Home > Mobile >  i cant clear one of two inputs
i cant clear one of two inputs

Time:03-11

i cant clear name input when i go into strict mode console say: "Cannot create property 'value' on string ''"

    const number = document.getElementById("number").value;
    const name = document.getElementById("name").value;

    console.log(name)
    console.log(number)

    if (name.trim() && number.trim()) {
        let contact = {
            name,
            number
        };
        renderContact(contact);
        console.log(contacts);
        contacts.push(contact)
    }
    // clearInput(number, name);
    this.name.value = '';
    this.number.value = '';
};

CodePudding user response:

Your "name" and "number" is not DOM elements. There's their values like: "Hello, World" and other. String is primitive type and you can't define properties in prototype in strict mode.

const numberInput = document.getElementById("number")
const nameInput = document.getElementById("name")

const name = numberInput.value
const number = numberInput.value

if (name.trim() && number.trim()) {
    let contact = {
        name,
        number
    };
    renderContact(contact);
    console.log(contacts);
    contacts.push(contact)
}
// clearInput(numberInput, nameInput);
nameInput.value = '';
numberInput.value = '';

};

  • Related