1

I have input button , what I want is if a user clicks on the button then textbox should appear.

Below is the code which is not working :

<input type="submit" value="Add Second Driver" id="driver" />
                    <input type="text" id="text" />

$("#driver").click(function() {

    $('#text').show();

  }
 });

Also the textbox should not be visible initially

7 Answers 7

3

You can use toggle instead;

$('#text').toggle();

With no parameters, the .toggle() method simply toggles the visibility of elements

Sign up to request clarification or add additional context in comments.

Comments

2

try this:

$(document).ready(function(){
    $("#driver").click(function(){
       $("#text").slideToggle("slow");
  });
});

1 Comment

give both of them in one 'div' and do slidetoggle for that 'div' instead of 'textbox'
2

Here's an example using toggle:

http://jsfiddle.net/x5qYz/

Comments

1
<input type="submit" value="Add Second Driver" id="driver" />
<input type="text" id="text" style="display:none;" />

$("#driver").click(function() {
    $('#text').css('display', 'block');
});

Comments

1
$(function()
{

    // Initially hide the text box
    $("#text").hide();

    $("#driver").click(function()
    {

         $("#text").toggle();
         return false; // We don't want to submit anything here!

    });

});

Comments

1

Try this:

<script type="text/javascript">
jQuery(function ($) {
    $('#driver').click(function (event) {
        event.preventDefault(); // prevent the form from submitting
        $('#text').show();
    });
});
</script>
<input type="submit" value="Add Second Driver" id="driver" />
<input type="text" id="text" />

2 Comments

can I show the textbox and label in a slow motion .. and yeh i dont want the form to be submit .
Yes. Just change the .show() to .show('slow'). Instead of the default show effect you can use fadeIn or slideDown.
1

Make the textbox hidden when the page loads initially like this

Code:

$(document).ready(function () {
$('#text').hidden();
 });

Then your should work the way you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.