I'm trying to compare two equal strings: a textarea
value (or textContent
, or innerHTML
) and a string stored as an attribute in Backbone model, e.g. "A string↵with line break"
.
And this comparing always returns false
.
Comparing length of these strings reveals the difference (the stored one is one symbol longer).
The question is how to prepare the first string (extracted from textarea
) to make it completely equal to the second one (stored in model).
P.S. They are both typeof === 'string'
.
P.P.S. The main problem is how to make Backbone see the equality while setting an attribute:
this.model.set({ attr: textareaValue })
.
Backbone uses Underscore's method which simply compares two strings in this case:
return '' + a === '' + b;
I've applied encodeURIComponent
on both strings: the result is Some%0Atext
vs Some%0D%0Atext
. So the second one has \r
character (it's rendered by Handlebars). Should I insert this character before each \n
?
P.P.P.S. Yes, this did the trick: textarea.value.replace(/\n/gm, '\r\n');
The problem was in \r
character: textarea value rendered by Handlebars was Some\ntext
while string stored in model was Some\r\ntext
).
And this did the trick: textarea.value.replace(/\n/gm, '\r\n');