Search code examples
javascriptobjectonclickhref

javascript object object on a href="javascript:..." call


I do have a simple script which is not working properly or as expected.

<script>
function modify_val(target, valeur)
{
    $(target).val(valeur)
}
</script>


<a href="javascript:modify_val('#type1','$row->movie2');">$row->movie2 ($row->counter relations)</a>

The javascript is working properly as the targeted input value is modified but the page is "redirected" after the click with a page containing:

[object Object]

Why is there a print ? I don't understand... The OnClick method behave the same. href or OnClick="javascript:$('#type1').val('$row->movie2');" also behave the same.

Do you have any idea ?

PS : the page is in https mode.


Solution

  • The return value of an event handler determines whether or not the default browser behaviour should take place as well.

    <script>
    function modify_val(target, valeur)
    {
        $(target).val(valeur);
        return false;
    }
    </script>
    

    Change your HTML as. I would suggest you to use onClick attribute

    <a href="#" onClick="return modify_val('#type1','$row->movie2');">$row->movie2 ($row->counter relations)</a>
    

    Demo