Johnny Doe
Johnny Doe

Reputation: 193

What's the difference if you use a "+" or no "+" in outputting?

Div1.InnerHtml = (someString);

-VS-

Div1.InnerHtml += (someString);


I notice they both do the same thing, but is there any real difference whether I have the + in there or not?
Also.. What's the difference between InnerText & InnerHtml?

Upvotes: 3

Views: 113

Answers (6)

Priyank Doshi
Priyank Doshi

Reputation: 13151

+= is a shorthand operator

for e.g.

i = i + (some value) and i+=(some value) both is same.

So, in general term,

Left_Val (operator)= right_val and Left_Val = Left_Val (operator) right_val are same

Please note that operator should be binary. shorthand operators cannot be used with unary (like unary minus) and ternary operator(like ?:).

Upvotes: 1

Pranay Rana
Pranay Rana

Reputation: 176896

Answer of first question

C# String Append -: += append string with the existing string

other example is

string s = "abc";
s+="cde";

output
s = "abcde"

Answer of second question

innerText will retrieve the value as it, though if it contains the tags, it will render the text as it is, where as innerHTML retrives the values by applying the HTML tags if any.

Upvotes: 4

STW
STW

Reputation: 46366

+= will append (someString) to the existing value of Div1.InnerHtml, whereas = will replace the value of Div1.InnerHtml with (someString).

If the results are the same then the starting value of Div1.InnerHtml is likely null or string.Empty ("")

Regarding InnerText vs InnerHtml: InnerHtml might return something like <h1>Hello World</h1> whereas InnerText would return Hello World (the value of the element without the actual HTML element).

Consider these cases:

string someString = "Hello";
string innerHtml = "";

innerHtml += someString; // result will be "Hello"

string someString = "Hello";
string innerHtml = "";

innerHtml = someString; // result will be "Hello"

string someString = "Hello";
string innerHtml = "World";

innerHtml += someString; // result will be "HelloWorld"

string someString = "Hello";
string innerHtml = "World";

innerHtml = someString; // result will be "Hello"

Upvotes: 6

Eganr
Eganr

Reputation: 670

  1. += is concatenation (shortform for a = a + b)

    = will overwrite the value with the new one on the righthand side

  2. innerHtml will allow you to add tags like <p> and div

    innerText will encode those tags as &lt;p&gt; and &lt;div&gt;

Upvotes: 1

evanmcdonnal
evanmcdonnal

Reputation: 48086

They're only doing the same thing because Div1 is empty when you do +=

+= is appending so if you did that a few times in a row you would see that you're getting data you may not have expected.

The assignment operator (=) will simply set the value of Div1 to whatever's on the left regardless of what was there previously.

Upvotes: 1

YavgenyP
YavgenyP

Reputation: 2123

They are not the same thing at all. This is about strings manipulation and not something specific and related to ASP.NET
The first puts someString in the string property InnerHtml of Div1,
The second sets InnerHtml to be InnerHtml +(someString), so if, for example InnerHtml contained the string "aa" before this operation, it'll become "aaSomeString" at the later case, and "SomeString" at the former case.

Upvotes: 2

Related Questions