0

i use jquery to add a full line to an existing table. Unfortunately the website jumps to the very top after this (as if i would reload it completely).

My code is:

<a href="#" onclick="addParam()">foo</a>

And the JS:

    function addParam() {
        $('#request_params tbody').append('<tr><td><input class="form-control paramcomplete" type="text" name="input_param_name" size="40" /></td><td><input class="form-control" type="text" name="input_param_value" size="40"/></td><td> <a href="#" onclick="removeParam(this)"><img src="/SequenceControl/pics/remove.png" style="width:20px" /></a></td></tr>');

        return false;
    }

Is there a possibility to make it stay where it is when i clicked on the link?

tuxmania
  • 906
  • 2
  • 9
  • 28

5 Answers5

3

also, you can do like href="javascript:void(0);"

or you can do like following

function addParam(e)
{
    // do your stuff
    e.preventDefault(); // Cancel the default action
});
Pranav Patel
  • 1,541
  • 14
  • 28
1

Just remove the href tag from the HTML. You don't need it!

Sampgun
  • 2,822
  • 1
  • 21
  • 38
0

Just remove href="#". It causes the jump to top

What is href="#" and why is it used?

Constantine
  • 544
  • 5
  • 15
0

You need to cancel out the default action so use onclick="return addParam()",

However I would recommend you to use correct semantic element; <button> instead of anchor and also as you are using jQuery use unobtrusive event handler

HTML

<button class="addRow" type="button" onclick="addParam()">foo</button >

Script

$('.addRow').on('click', function(){
     //Your code
})
Satpal
  • 132,252
  • 13
  • 159
  • 168
0

You have mentioned href="#" in anchor tag, which doesn't specify an id name, so clicking an anchor with href="#" will move the scroll position to the top.

remove href attribute from anchor tag.

<a onclick="addParam()">foo</a>
Arvind
  • 19
  • 5