jquery - display random text from a list on submit from textarea -
i'd achieve following:
i have text area input ( button type).
the user fill text area, otherwise, on submit, text being display ( in div) should different ( taken randomly list or other word ).
on moment have following:
<div class="row"> <div clas="col-md-12"> <form name="myform"> content added: <textarea name="mycontent">add name</textarea> <input type="button" value="add content" onclick="addcontent('result', document.myform.mycontent.value); setcookie('content', document.myform.mycontent.value, 7);"> </form> </div> </div> <script> function addcontent(divname, content) { document.getelementbyid(divname).innerhtml = content; } </script>
which make appear text added in area in other div. have no idea of path should take display text fro specific list randomly instead.
any highlight, amazing !
thank time
first of all, 've tag question jquery tag. i'll respond jquery way.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- above 3 meta tags *must* come first in head; other head content must come *after* these tags --> <title>bootstrap 101 template</title> <!-- bootstrap --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <h1>hello, world! <span name="result"></span></h1> <div class="row"> <div clas="col-md-12"> <form name="myform" id="myform"> content added: <textarea name="mycontent" id="mycontent">add name</textarea> <input type="button" value="add content" id="mysubmitbtn"> </form> </div> </div> <!-- jquery (necessary bootstrap's javascript plugins) --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <!-- include compiled plugins (below), or include individual files needed --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <script> // dom ready $(function(){ myarray = ['toto', 'tutu', 'titi', 'me', 'her !! ']; console.log("ready work jquery"); //listener on sumbit btn (ie : old onclick function) $("#myform").on("click","#mysubmitbtn", function(){ randomize(); addcontent('result', $("#mycontent").val()); //setcookie('content', document.myform.mycontent.value, 7); }); randomize = function(){ var randomnbr = math.floor(math.random() * myarray.length); if( $("#mycontent").val() === ""){ $("#mycontent").val(myarray[randomnbr]); } }; function addcontent(divname, content) { console.log('hello : '+content); $("[name='"+divname+"']").text(content); } }); </script> </body> </html>
so, can see :
- i create bootstrap page can test code
- i have put id on input it's easier access them in jquery
- i create array you'll put future pseudo (i guess)
- i make listener submit click event
- i create random function put pseudo in input textearea if textarea empty !!
just copy code in html file test !!
Comments
Post a Comment