javascript - onclick function not working at first click - Stack Overflow

i tried to set up a onclick function on a "reset" button type, which will only work if i clic

i tried to set up a onclick function on a "reset" button type, which will only work if i click it twice, i've even tried it as a normal button type with the same results. I've placed it on other parts of the site but the same results persists.

The functions it does is that it erases out text that appears dynamically everytime you add a new order. And the html reset functions wipes out the rest of the sites input, which is ok i guess.

Here you can visit the website and see for yourself:

.php

Here is a part of the code,

var tester;
var tester = function () {
    document.form['reset'].onclick = function () {
        contact_box_kurv.innerHTML = ("").replace (/,/g, ''); 
        return false;
    };
};
</script>

<h2><INPUT TYPE="RESET" name="reset" id="reset" VALUE="Tøm Handlekurv" onclick="tester()"></h2>

Note: To see the function work on the website you will need to add an order.

i tried to set up a onclick function on a "reset" button type, which will only work if i click it twice, i've even tried it as a normal button type with the same results. I've placed it on other parts of the site but the same results persists.

The functions it does is that it erases out text that appears dynamically everytime you add a new order. And the html reset functions wipes out the rest of the sites input, which is ok i guess.

Here you can visit the website and see for yourself:

http://www.premiere-produkter.no/pp/lagersalg/index.php

Here is a part of the code,

var tester;
var tester = function () {
    document.form['reset'].onclick = function () {
        contact_box_kurv.innerHTML = ("").replace (/,/g, ''); 
        return false;
    };
};
</script>

<h2><INPUT TYPE="RESET" name="reset" id="reset" VALUE="Tøm Handlekurv" onclick="tester()"></h2>

Note: To see the function work on the website you will need to add an order.

Share edited Jan 24, 2013 at 8:10 Tepken Vannkorn 9,72314 gold badges62 silver badges86 bronze badges asked Jan 24, 2013 at 8:06 TommyTommy 833 silver badges10 bronze badges 3
  • Simply because you are using onclick twice.. – Shurmajee Commented Jan 24, 2013 at 8:10
  • Removing mas from an empty string? – Musa Commented Jan 24, 2013 at 8:11
  • @Musa - Better safe than, sorry. You, never know, where mas, are going to spring, up. ,,, – nnnnnn Commented Jan 24, 2013 at 8:12
Add a ment  | 

2 Answers 2

Reset to default 4

Your button has an inline onclick attribute that calls tester(). So on first click it'll do whatever tester() does - which is replace that with another onclick set to the anonymous function inside tester(). So then on second click it'll do what that second anonymous function does.

Try this instead:

var tester = function () {
    contact_box_kurv.innerHTML = ("").replace (/,/g, ''); 
    return false;
};

(I.e., have the desired functionality directly in tester().)

You need to put this script separately:

window.onload = function ()
{
    document.form['reset'].onclick = function () {
    contact_box_kurv.innerHTML = ("").replace (/,/g, ''); 
    return false;
    };
};

Remove the event handler script.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742262322a4411137.html

相关推荐

  • javascript - onclick function not working at first click - Stack Overflow

    i tried to set up a onclick function on a "reset" button type, which will only work if i clic

    21小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信