ntmw Posted August 16, 2011 Share Posted August 16, 2011 I'd like to create an idea generator that pulls from different lists. It looked like using an array index would be the best option. I ran the jslint and it said the code was valid, but I just don't get why it's not outputting anything. Any suggestions would greatly be appreciated. <html> <form action="java script:" id="generator" name="generator"> <input type="button" value="click" onClick="generate();"> <br><br> <!--<p id="idea" ></p> --> <input id='idea' name="idea"> </form> </html> function generate(e) { var keywords = [ "keyword A", "keyword B", "keyword C", "keyword D", "keyword E", "keyword F", "keyword G" ]; var keyword = keywords(math.floor(math.random() * keywords.length)); document.getElementById("idea").onClick = keyword;}document.getElementById("idea").onClick = generate;document.getElementById("generator").onClick = generate; I've also got it sitting at JSFiddle if that helps: http://jsfiddle.net/ntmw/yG8ya/4/ Link to comment Share on other sites More sharing options...
JamesB Posted August 16, 2011 Share Posted August 16, 2011 to read a value in an array you put the key in [ and ]: var keyword = keywords[math.floor(math.random() * keywords.length)]; also in this line: document.getElementById("idea").onClick = keyword; the onClick property should take a function, maybe you meant ".value =" Link to comment Share on other sites More sharing options...
thescientist Posted August 16, 2011 Share Posted August 16, 2011 also, passing JSLint valisation does not guarantee that just because code is syntactically correct and well formatted, that it would be free from logic errors. Link to comment Share on other sites More sharing options...
tinfanide Posted August 17, 2011 Share Posted August 17, 2011 I'd like to create an idea generator that pulls from different lists. It looked like using an array index would be the best option. I ran the jslint and it said the code was valid, but I just don't get why it's not outputting anything. Any suggestions would greatly be appreciated. <html> <form action="java script:" id="generator" name="generator"> <input type="button" value="click" onClick="generate();"> <br><br> <!--<p id="idea" ></p> --> <input id='idea' name="idea"> </form> </html> function generate(e) { var keywords = [ "keyword A", "keyword B", "keyword C", "keyword D", "keyword E", "keyword F", "keyword G" ]; var keyword = keywords(math.floor(math.random() * keywords.length)); document.getElementById("idea").onClick = keyword;}document.getElementById("idea").onClick = generate;document.getElementById("generator").onClick = generate; I've also got it sitting at JSFiddle if that helps: http://jsfiddle.net/ntmw/yG8ya/4/ Test this:<html><script>function generate() { var keywords = [ "keyword A", "keyword B", "keyword C", "keyword D", "keyword E", "keyword F", "keyword G" ]; var rand = Math.floor(Math.random() * keywords.length-1); document.getElementById("idea").innerHTML = keywords[rand];}</script> <input type="button" value="click" onClick="generate();"> <br><br> <p id="idea" >Random ideas (keywords) are generated here once you click the "generate" button</p> </html> Maybe ya can think back about what ya've coded.Ignore the random part and I think ya can kinda tidy up the rest first. The rest (the generate function) is messy.Like ya don't have to pass the "e" parameter as ya don't include anything in the onClick="generate( nothing is here)". Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.