JQuery to Native Javascript (simple click call function) -
i'm trying convert following jquery native javascript, can't seem work.
any appreciated.
jquery:
$(document).ready(function() { $("button").click(function() { var char = "0123456789abcdefghijklmnopqrstuvwxyz"; var fullchar = "0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; var genhash = ""; var i; (i = 0; < 8; i++) { var rnum = math.floor(math.random() * char.length); genhash += char.substring(rnum, rnum + 1); } $("input").val(genhash); }).click(); });
native javascript:
function passwordgenerator() { var char = "0123456789abcdefghijklmnopqrstuvwxyz"; var fullchar = "0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; var genhash = ""; var i; (i = 0; < 8; i++) { var rnum = math.floor(math.random() * char.length); genhash += char.substring(rnum, rnum + 1); } document.getelementsbytagname("input").value = genhash; } document.getelementsbytagname("button").onclick = function() { passwordgenerator(); }; passwordgenerator();
function passwordgenerator() { var char = "0123456789abcdefghijklmnopqrstuvwxyz"; var fullchar = "0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; var genhash = ""; var i; (i = 0; < 8; i++) { var rnum = math.floor(math.random() * char.length); genhash += char.substring(rnum, rnum + 1); } document.getelementsbytagname("input").value = genhash; } document.getelementsbytagname("button").onclick = function() { passwordgenerator(); }; passwordgenerator();
<input type="text" /> <button> generate password </button>
try adding index
[0]
of element within htmlcollection
returned document.getelementsbytagname()
function passwordgenerator() { var char = "0123456789abcdefghijklmnopqrstuvwxyz"; var fullchar = "0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; var genhash = ""; var i; (i = 0; < 8; i++) { var rnum = math.floor(math.random() * char.length); genhash += char.substring(rnum, rnum + 1); } // add index `[0]` of element within `htmlcollection` after call // `.getelementsbytagname("input")` document.getelementsbytagname("input")[0].value = genhash; } // add index `[0]` of element within `htmlcollection` after call // `.getelementsbytagname("button")` var button = document.getelementsbytagname("button")[0]; button.addeventlistener("click", function() { passwordgenerator(); }); // call `click` event on `button` button.click();
<input type="text" /> <button> generate password </button>
Comments
Post a Comment