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
  • 896
  • 2
  • 8
  • 26

5 Answers5

4

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,852
  • 11
  • 26
1

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

Sampgun
  • 2,660
  • 1
  • 17
  • 35
0

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

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

Constantine
  • 504
  • 4
  • 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
  • 129,808
  • 12
  • 152
  • 166
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