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
Christopher Nelson
Front End Web Development Techdegree Student 13,162 PointsTrying to make div appear based on form input.
I have three different buttons that are hidden. based on a users input I want a button to appear and the other two to stay hidden. I am completely lost regarding the right way to go about doing this.
Here is my HTML file:
<form id='submit_button' >
<input type='text'>
<input type="button" value="submit">
</form>
<div id='Small_box'>
</div>
<div id= 'Medium_box'>
</div>
<div id= 'Large_box'>
</div>
Here is my JavaScript file:
$(document).ready(
$('#Small_box').hide();
$('#Medium_box').hide();
$('#Large_box').hide();
$('#submit_button').focus( function(){
if((this).val() >= 1 && <= 20) {
$('#Small_box').show();
}
else if((this).val() >= 21 && <= 30){
$('#Medium_box').show();
}
else if((this).val() >= 31 && <= 40){
$('Large_box').show();
}
else(
Alert('This is not a valid box number')
)
});
);
1 Answer
LaVaughn Haynes
12,397 PointsGive this a shot
<!DOCTYPE html>
<html>
<head>
<title>Sign Up Form</title>
<style>
.boxes{
display: none;
}
</style>
</head>
<body>
<form id='submit_button'>
<input type='text' class="boxsize">
<input type="submit" value="Submit Box Size">
</form>
<div id='small_box' class="boxes">Small Box</div>
<div id='medium_box' class="boxes">Medium Box</div>
<div id='large_box' class="boxes">Large Box</div>
<div id="error_box" class="boxes"></div>
<script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
<script>
var $small = $('#small_box');
var $medium = $('#medium_box');
var $large = $('#large_box');
var $error = $('#error_box');
var $boxes = $('.boxes');
//the boxes are hidden by the css up top though you
//can also do it with javascript like so:
//$('.boxes').hide();
$('#submit_button').on('submit', function(e){
//stop the form from submitting
e.preventDefault();
//get the input value from the user
var $boxsizeInput = $(this).find('.boxsize');
//convert it from a string to a number
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
var boxsize = parseInt($boxsizeInput.val(), 10);
//hide all of the boxes
$boxes.hide();
//test the sizes and show the right box or an error
if(boxsize >= 1 && boxsize <= 20){
$small.show();
}else if(boxsize >= 21 && boxsize <= 30){
$medium.show();
}else if(boxsize >= 31 && boxsize <= 40){
$large.show();
}else{
$error.html('"' + $boxsizeInput.val() + '" is not a valid box number').show();
}
//clear the input
$boxsizeInput.val('').focus();
});
</script>
</body>
</html>
LaVaughn Haynes
12,397 PointsLaVaughn Haynes
12,397 PointsObviously also let the use know the range of valid numbers (1-40)