2

I am using this code to get the current url of the page in to div, when I click on submit button. I am not getting how to call function onclick. How to do this?

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>localhost</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
        <script type="text/javascript">
            $(function () {
                var url = $(location).attr('href');
                $('#spn_url').html('<strong>' + url + '</strong>');
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div id="spn_url"></div>
            <input type="submit" value="submit" name="submit">
        </form>
    </body>
</html>
arulmr
  • 8,620
  • 9
  • 54
  • 69
milano
  • 465
  • 5
  • 13
  • 20

5 Answers5

8

Please have a look at http://jsfiddle.net/2dJAN/59/

$("#submit").click(function () {
var url = $(location).attr('href');
$('#spn_url').html('<strong>' + url + '</strong>');
});
vinothini
  • 2,606
  • 4
  • 27
  • 42
6

try this ..

HTML

<input type="submit" value="submit" name="submit" id="submit">

jQuery

$(document).ready(function () {
    $('#submit').click(function () {
        var url = $(location).attr('href');
        $('#spn_url').html('<strong>' + url + '</strong>');
    });
});
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
  • 3
    **+1** `$(location).attr('href');` could be `location.href`, no need to pass to jQuery :) –  Jun 07 '13 at 05:42
4

Try this:

HTML:

<input type="submit" value="submit" name="submit" onclick="myfunction()">

jQuery:

<script type="text/javascript">
 function myfunction() 
 {
    var url = $(location).attr('href');
    $('#spn_url').html('<strong>' + url + '</strong>');
 }
</script>
leek
  • 11,803
  • 8
  • 45
  • 61
Bharat Chodvadiya
  • 1,644
  • 4
  • 20
  • 31
2

try this:

$('form').submit(function(){
    // this function will be raised when submit button is clicked.
    // perform submit operations here
});
Zain Shaikh
  • 6,013
  • 6
  • 41
  • 66
1

JS

 $(function () {
    var url = $(location).attr('href');
    $('#spn_url').html('<strong>' + url + '</strong>');
    $("#submit").click(function () {
        alert('button clicked');
    });
});

html

<input id="submit" type="submit" value="submit" name="submit">
Sudarshan Tanwar
  • 3,537
  • 3
  • 24
  • 39