0

I'm receiving a console error for this line:

$("#note" + noteCount).val() = note;

Here is the full function:

function addNotes() {
    noteCount++;
    var note = $("#noteInput").val();
    console.log("Note: " + note);
    console.log("Note Count: " + noteCount);
    var display = document.createElement("div");
    document.getElementById("displayContainer").appendChild(display);
    display.className = "noteDisplay";
    display.id = "note" + noteCount;
    $("#note" + noteCount).val() = note;
}

As you can see i'm creating a div with id 'note' + note count but i'm not sure how to select it to change its value.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Xander
  • 991
  • 1
  • 13
  • 32
  • Possible duplicate of [ReferenceError: Invalid left-hand side in assignment](http://stackoverflow.com/questions/18597667/referenceerror-invalid-left-hand-side-in-assignment) – Alexei Levenkov Apr 14 '17 at 15:34

4 Answers4

2

Better use textContent property as you are creating a DIV

 var display = document.createElement("div"); 
 display.id = "note" + noteCount;
 display.textContent = note;

OR, .text() as DIV doesn't have value property and you need to provide it as a parameter to the method call:

$("#note" + noteCount).text(note);

As you are using jQuery create HTML using it

var display = $("<div>", {
    "text" : note,
    "id" : "note" + noteCount,
    "class" : "noteDisplay"     
});

display.appendTo('#displayContainer');
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • 2
    Well spotted that it's a `div` element. I'd expand the answer to improve the logic to get rid of the `createElement`/`appendChild` ugliness too. – Rory McCrossan Apr 06 '17 at 10:19
1

Simply try

$("#note" + noteCount).val( note );

val invocation returns a value which you cannot override with another value.

val = note; //has a different meaning, still not correct for your scenario

but

val() = note; //not allowed 

since value is already computed via function call and hence left side is not a variable , but a value and you cannot assign a value to a value

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
1

Your way of assigning the value is wrong . You need to pass it as an argument to the val function because its a function and not a property

$("#note" + noteCount).val(note) ;

See the JQUERY DOCS

Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
  • I was confused when looking at the doc previously becuase it says 'this method does not accept any arguments'. Thank you though :) – Xander Apr 06 '17 at 10:09
  • No Problem :). Glad to help – Shubham Khatri Apr 06 '17 at 10:12
  • I will in just a second yes :) I'm sure you're right but when I test it out it's not being displayed. The div is being created with the id and class name but the class isn't taking effect either – Xander Apr 06 '17 at 10:14
1

You should put the value inside () of val when using jQuery;

$("#note" + noteCount).val(note);
Mamun
  • 66,969
  • 9
  • 47
  • 59