javascript - getElementById doesn't work correctly -


here code - http://jsfiddle.net/ec6jt/

<input type="text" id="mytext"> <a href='javascript:elem();'>click</a><br/>    <input type="text" id="mytext2"> <a href='javascript:document.getelementbyid("mytext2").value = "my default value";'>click</a><br/>  <script type="text/javascript"> function elem() {   document.getelementbyid("mytext").value = "my default value"; }; </script> 

why first "click" link works correctly , second 1 doesn't? why second "click" link erases everything?

thanks.

instead of this

<a href='javascript:document.getelementbyid("mytext2").value = "my default value"; '>click</a><br/>  

use this

<a href='javascript:document.getelementbyid("mytext2").value = "my default value"; return false;'>click</a><br/>  

excellent explanation @jonathan lonowski

to explain need return false or similar, javascript:... display result if it's truthy -- e.g. href="javascript:"foo". elem() returns undefined default, falsy. but, assignment operator (=) returns value being assigned. option prefix statement void operator discard possible results -- href="javascript:void document....". –


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -