Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Using jQuery to form a random string?

I'm working on a problem that asks me to choose:

Hi All,

I'm a little jammed up on this problem. I know it should be simple but I'm not making the connection yet. Any simple solutions would be appreciated!

1) Choose a jQuery method.

2) Create a website that displays a random string when you click a button. This string should show in a DOM element - e.g. in the page itself - instead of in an alert or on the console. You'll be using the function you just wrote above to generate that random string.

  • Use jQuery to do this. Submit the HTML and JavaScript used -

Since this is looks like your CS homework I feel I can't give you a direct answer, but I will give you some hints,

  1. an array

  2. a decrementing function for each of your array items, and return them.

  3. something that selects the button used.

If you have code already done post it up and we will be happy to help with any errors and such, but because this looks like real class work, I feel it would not be beneficial to you to just give an answer. I hope you understand where I am coming from.

2 Answers

makes sense. I'll repost with my working solution!

Please do, and let's see what you come up with and work off that. We are happy to help.

Here we go. I am able to generate a random string but unless I refresh the browser I can't click the button for a random string again. Any ideas?

//html
<!DOCTYPE html>
<html>
    <head>
      <meta charset="utf-8">
    <title>Random Generator</title>

  </head>
  <body>
        <h1>Hello World</h1>
        <p><button>Click here to randomly generate a string</button></p>

        <script src="jquery-1.11.2.js"></script>
        <script src='randomString.js'></script>
  </body>
</html>

//JavaScript

$('p').click (function makeid(){
   var text = "";
   var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

   for( var i=0; i < 5; i++ )
       text += possible.charAt(Math.floor(Math.random() * possible.length));
     return document.write(text);

});

Did you ever get it to work?

You're very close, Look at my message before in bold and that will give you a hint as far as the Jquery method()

You need to be able to loop through the variable something that attaches to the variable like var possible[something % something.length]

also you need to loop through the click maybe a count loop might work. you know what var i++ is, what does ++i do?

oh and you don't need the p tags, just use the button. That's that is there for.

Lastly instead of document.write() try and use innerHTML its nicer.

Great Job on your first attempt!!

Let me know if you get stuck, but you should be able to do it with the information above.

Hint: forget the for loop.