0

I am developing an asp.net site.

I am using

document.getElementById("< %=TextBox1.ClientID %>").value = "";

I called the JavaScript function and the value did look disappear but when I inspect it the value is still there...

e.g. I see value="previousentry"

Is this related to "EnabledViewState"? How do make sure the value will be gone after calling the above JavaScript?

ADyson
  • 57,178
  • 14
  • 51
  • 63
cvvw
  • 5
  • 2
  • Don't know if this is a copy and paste mistake but....`document.getElmentById` spelling mistake? – JamesS May 04 '20 at 13:33
  • Also what is`"< %=TextBox1.ClientID %>"`? Why can't you just use the id? – JamesS May 04 '20 at 13:34
  • @JamesS, It is the standard way of getting the control id. He has done it fine with it. – Jamshaid K. May 04 '20 at 13:37
  • sorry, my typo, should be "document.getElementById", done amended – cvvw May 04 '20 at 13:52
  • `< %=` looks like maybe a typo. Try `<%=` . Also, the `value` _attribute_ can be set in the HTML, but `.value` amends the related _property_ in the DOM. There is a subtle difference. So expecting the value to vanish in the HTML may not be the correct expectation. You can test the real current value of the element by writing `console.log(document.getElementById("<%=TextBox1.ClientID %>").value);` on the next line, and then checking your Console. – ADyson May 04 '20 at 14:08
  • And no this has nothing to do with ViewState (unless you have refreshed the page in between clearing the value and inspecting the HTML). – ADyson May 04 '20 at 14:11
  • @ADyson thanks for your comment.....how do I make sure the value has been set to "" or the value data is killed... after console log....errors come out in the log... cannot read property 'value' of null – cvvw May 04 '20 at 14:20
  • if the element object is null, that suggests that `"<%=TextBox1.ClientID %>"` did not resolve to a real element in the page. – ADyson May 04 '20 at 14:26
  • @JamesS That syntax (or something close to it, if you fix the typo ADyson pointed out) is a typical way of getting the client ID of an ASP.NET Web Forms element into the rendered markup. By default, [ClientIDMode](https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.control.clientidmode) settings will changed the ID declared in the control from what you declared it in the .aspx markup to something else in the rendered HTML. It's a source of a lot of confusion in Web Forms (and one of many reasons Web Forms should be avoided). – mason May 04 '20 at 14:53
  • https://www.w3schools.com/howto/tryit.asp?filename=tryhow_html_clear_input2 referring to this example, after using the function .value = ''", the value is still there(using inspect can see).... – cvvw May 04 '20 at 15:01
  • yep, as I said, that's normal. If we use another version of that demo, and use the button to change the value to something else (so it's clear to see), and then log it to the console, you can see that the original _attribute_ in the HTML remains the same, but the _property_ of the related DOM element is changed (you can see it on the console). See here: https://jsfiddle.net/ehgzbkt8/ . When the form is posted back, it will be the property value which is sent to the server. The attribute value is only the value which was used to set it up when the page was first loaded from the HTML document. – ADyson May 04 '20 at 15:09
  • Further reading: https://stackoverflow.com/questions/6003819/what-is-the-difference-between-properties-and-attributes-in-html (DOM stands for Document Object Model. More here: https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction) – ADyson May 04 '20 at 15:11
  • @ADyson, thanks for your clear example, is there anyway we can force to change the value (value="Blabla") using javascript – cvvw May 04 '20 at 15:17
  • What do you mean?? The value **is** being changed. I've explained to you why it doesn't show in the element inspector. That is the wrong place to look. – ADyson May 04 '20 at 15:19

0 Answers0