2

I need to redirect to a new page on a button click. I need to write this using jquery. I don't know how to write the code for that. Here is my HTML

 <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
   <a class="btn btn-success" style="width: 100%" id='approve'>Approve</a>
</div>

and the script i wrote is

  $(document).ready(function() {
            //LIVEICON
            $('#approve').on('click',function(){
                var comment = $('#comments').val();
                if(($('#comments').val()!="")){                             
                    alert(comment);
                    $('#successalert').html('<strong>Approved...!</strong>').show().fadeOut(5000); 

                    }
                else{

                    if($("#comments").val()==""){$('#comments').css({'background-color':'rgba(255, 0, 0, 0.09)','border-color':'red'});$('#comments_er').css('display','block');}
                    if($("#approver").val()==null){$('#approver').css({'background-color':'rgba(255, 0, 0, 0.09)','border-color':'red'});$('#approver_er').css('display','block');}
                }
            });
        });
        $('#comments').change(function(){
            if ($('#comments').val()!="") {$('#comments').css({'background-color':'#fff','border-color':'#ccc'});$('#comments_er').css('display','none')}
            if ($('#comments').val()=="") {$('#comments').css({'background-color':'rgba(255, 0, 0, 0.09)','border-color':'red'});$('#comments_er').css('display','block')}

        });
        $('#approver').change(function(){
            if ($('#approver option:selected').val()!=null) {$('#approver').css({'background-color':'#fff','border-color':'#ccc'});$('#approver_er').css('display','none')}
            if ($('#approver option:selected').val()==null) {$('#approver').css({'background-color':'rgba(255, 0, 0, 0.09)','border-color':'red'});$('#approver_er').css('display','block')}

        });

What is the issue with this??

Thanks in advance

Shanmugapriya
  • 57
  • 3
  • 15
  • Possible duplicate of [How to create an HTML button that acts like a link?](https://stackoverflow.com/questions/2906582/how-to-create-an-html-button-that-acts-like-a-link) – Josh Whitlow Oct 06 '17 at 10:52
  • @JoshWhitlow Hi john.. You see the code now.. Its not duplicate. I am having a different issue – Shanmugapriya Oct 06 '17 at 11:07

5 Answers5

1

you can use simple javascript with in jquery click event.

here is how your code will look like.

$('#link').click(function(){
  alert("Clicked");
  window.location.href="http://smashingmagazine.com";
});
cool cool
  • 247
  • 2
  • 12
0

Why not using simple HTML? You could make it dynamic with echo in PHP.

HTML:

<form action="yourlink"><input type="submit"></form>

To open a new tab:

<form action="yourlink" target="_blank"><input type="submit"></form>

Javascript:

<input type="button" onclick="location.href='yourlink';"> 

Your code within jQuery:

jQuery:

$('#link').click(function(){
  alert("Clicked");
  window.location.href="yourlink";
});

Html:

<button id="link" >Link</button>

Some information about onclick function: w3schools onclick

Ronnie Oosting
  • 1,252
  • 2
  • 14
  • 35
0

Why do you need to use jQuery? Can you not just:

<a href="mypage.html"><button>Button Text</button></a>
Scott Brown
  • 334
  • 1
  • 2
  • 13
  • Actually i need to do some validations. After finishing that validations only my page need to redirect. That's why i am using jQuery – Shanmugapriya Oct 06 '17 at 10:58
0

You can add to the button onclick="location.href='pageurl.html';"

ADIN
  • 317
  • 3
  • 14
0

Try the below code

$('#link').click(function(){
  alert("Clicked");
  document.location.href = 'http://smashingmagazine.com'
});
Aparajit P Utpat
  • 375
  • 3
  • 14