1

please help me with my snippet.. I have this code and I don't know what is wrong.. This is my code

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<form id="krishna">
    <input type="text" readonly value="krishna" ><br>
    <button id="next">Next</button>
</form>
<form id="radha">
    <input type="text" readonly value="radha" ><br>
    <button id="prev">Previous</button>
</form>
<script>
$(document).ready(function(e) {
    $("#radha").hide();

    $("#next").click(function() {
        $("#radha").show();
        $("#krishna").hide();
    });

    $("#prev").click(function() {
        $("#radha").hide();
        $("#krishna").show();
    });
});
</script>

When I click next It seems does not go to the next form.. thank you all.

3
  • What error do you get? Commented Oct 1, 2013 at 4:47
  • jsfiddle.net/LzpER Is this your behavior? Commented Oct 1, 2013 at 4:50
  • @Krishna.. Looks like above code works fine for me.. Pls check this ... jsbin.com/uqUPipI/1/edit Commented Oct 1, 2013 at 4:50

6 Answers 6

4

This may be due to some missing attibutes of your form

HTML

<form id="krishna">
    <input type="text" readonly value="krishna" />
    <br />
    <button type='button' id="next">Next</button>
</form>
<form id="radha">
    <input type="text" readonly value="radha" />
        <br />
    <button type='button' id="prev">Previous</button>
</form>

Script

$(document).ready(function () {
    $("#radha").hide();

    $("#next").click(function () {
        $("#radha").show();
        $("#krishna").hide();
    });

    $("#prev").click(function () {
        $("#radha").hide();
        $("#krishna").show();
    });
});

check here

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

3 Comments

Might want to highlight what you've changed as it isn't completely obvious
thank you.. ill accept your answer and I actually found out that you can also use <a></a> instead of type button.. but i will still use your code.. thanks a lot
Oh, I just meant make a note stating you added type="button" to the button elements and maybe explain why that works
2

You need to prevent the default action, which is the form submit. When you prevent the default action from happening, you will see the desired result

Check this fiddle

$(document).ready(function () {
    $("#radha").hide();

    $("#next").click(function (e) {
        e.preventDefault();
        $("#radha").show();
        $("#krishna").hide();
    });

    $("#prev").click(function (e) {
        e.preventDefault();
        $("#radha").hide();
        $("#krishna").show();
    });
});

P.S I am not sure why you are still using jquery 1.3.x.

Also, check the fiddle for the updated HTML

Comments

0

Because it is trying to submit the form. So you need to make that button type to button. It is "submit" by default in a form.

JSFidle Demo

<form id="krishna">
<input type="text" readonly value="krishna" ><br>
<button type="button" id="next">Next</button>
</form>
<form id="radha">
<input type="text" readonly value="radha" ><br>
<button type="button" id="prev">Previous</button>
</form>

Comments

0

Your code is fine but What is happening here that your submit button required post request. So you can prevent this behavior by e.preventDefault() function of jquery. I have updated the jquery code as below.

    $(document).ready(function(e) {
$("#radha").hide();

$("#next").click(function(e){
    e.preventDefault();
$("#radha").show();
$("#krishna").hide();
});

$("#prev").click(function(e){
            e.preventDefault();
$("#radha").hide();
$("#krishna").show();
});

});

visit the following link to get demo on jsfiddle :

http://jsfiddle.net/4N95Q/

Comments

0

This is how I did it.

http://jsfiddle.net/GnzWZ/2/

$('form button').click(function(e){
    $("#radha, #krishna").toggle();
    return false;
});

Notes:

  • return false is the jquery solutions for preventdefault.
  • You can simplify your selectors and optimize your code a little more.
  • Avoid making style changes that should be originally made in CSS.

2 Comments

This would prevent clicking anywhere in the form which probably isn't desired
Just change the selector to form button then.
-1
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<div id="krishna">
    <input type="text" readonly value="krishna" ><br>
    <button id="next">Next</button>
</div>
<div id="radha">
    <input type="text" readonly value="radha" ><br>
    <button id="prev">Previous</button>
</div>
<script>
$(document).ready(function(e) {
    $("#radha").hide();

    $("#next").click(function() {
        $("#radha").show();
        $("#krishna").hide();
        console.log("next click");
    });

    $("#prev").click(function() {
        $("#radha").hide();
        $("#krishna").show();
    });
});
</script>

Working fine if you change

form

in the

div

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.