0

I want to have multiple buttons use the same function. Here What I'm trying to do.

<button><a herf="#" onClick="myFunction('clk1',c1);">button</a></button>
<p id="clk1"></p>  

With the buttons I want to change the variable and change the paragraph

<script>
    var c1 = 0;
    function myFunction(paragraph,varibl) {
            varibl += 1;
            document.getElementById(paragraph).innerHTML = varibl;
    }
</script>

I've looked around and can't find any thing. this doesn't work and I don't know how to make it work.

the full code:

<!DOCTYPE html>
<html>
<meta charset="utf-8"/>
<head>
    <title>clicker clicker</title>
    <style>
        html {
            text-align: center;
            background-color: #d1d1d1;
            font-family:Verdana;
        }
        .onebutton {
        background-color: #f4f4f4;
        border: 2px solid #5b5b5b;
        color: #5b5b5b;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 32px;
        cursor: pointer;
        border-radius: 8px;
        box-shadow:inset 0px 12px 22px 2px #ffffff;
        }
        .onebutton:active {
            background-color: #e5e5e5;
        }
        .twobutton {
        background-color: #f4f4f4;
        border: 2px solid #5b5b5b;
        color: #5b5b5b;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 16px;
        cursor: pointer;
        border-radius: 8px;
        box-shadow:inset 0px 12px 22px 2px #ffffff;
        }
        .twobutton:active {
            background-color: #e5e5e5;
        }
    </style>
</head>
<body>
    <h1>Clicker Clicker</h1>
    <p id="number"></p>
    <button class="onebutton"><a herf="#" onClick="clc();">click</a>
</button>
    <br>
    <p>clicker:100</p>
    <button class="twobutton"><a herf="#" 
onClick="upgrade('clk1',c1);">buy</a></button>
    <p id="clk1"></p>
    <p>clicker:1000</p>
    <button class="twobutton"><a herf="#" onClick="upgrade('clk2',c2);">buy</a></button>
    <p id="clk2"></p>
    <script>
        var number = 0;
        var c1 = 0;
        var c2 = 0;
        document.getElementById("number").innerHTML = number;

        function clc() {
            number += 1;
            document.getElementById("number").innerHTML = number;
        }
        function update() {
            number += c1;
            document.getElementById("number").innerHTML = number;
            c1 += c2;
            document.getElementById("clk1").innerHTML = c1;
        }
        function upgrade(what,clicker) {
            window[clicker] += 1;
            document.getElementById(what).innerHTML = clicker;
        }   
        setInterval(update, 100);
    </script>
</body>
</html>

this is here so it doesnt say i have to much code sdljnvaksjdnfblkajsdbfjmas dbfmha bsdmnfb admsf bds msadf

3
  • What seems to be not working ? Commented Sep 17, 2017 at 14:56
  • the variable dosn't change when the button is pressed Commented Sep 17, 2017 at 14:57
  • Check the below answer, I updated the example. Commented Sep 17, 2017 at 15:20

4 Answers 4

1

User can use object for easy modification of values inside function

<button><a herf="#" onClick="myFunction('clk1','c1');">button</a></button>
<p id="clk1"></p> 
..
<button><a herf="#" onClick="myFunction('clk1','c2');">button</a></button>
<p id="clk1"></p> 
..
<button><a herf="#" onClick="myFunction('clk1','c3');">button</a></button>
<p id="clk1"></p> 

and the script

<script>
    var c = {c1: 0, c2: 0, c3: 0}
    function myFunction(paragraph,varibl) {
            c[varibl] = c[varibl] + 1 
            document.getElementById(paragraph).innerHTML = c[varibl];
    }
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

If you have to pass the variable using the signature and can't simply (as per the other answers) directly reference the correct one in the function, here's what you do.

JS always passes a variable by value, not reference. However, if you send an object, the "value" is actually a reference to the original object. So you can do something like this:

var counters = { a: 0 };
function test(key) {
  counters[key]++;
  console.log(counters);
}
<button onclick="test('a')">Click</button>

2 Comments

this actually works, but this is really unnecessary coding. don't you agree?
True. But I don't usually assume we have the full context in SO questions - OP might have needed to use it this way for some reason.
1

do this:

<button><a herf="#" onClick="myFunction('clk1');">button</a></button>
<p id="clk1"></p> 

and then:

<script>
    var c1 = 0;
    function myFunction(paragraph) {
            c1++;
            document.getElementById(paragraph).innerHTML = c1;
    }
</script>

each time you call this method, the variable goes up by one! jsfiddle

3 Comments

thank you but i want to have more buttons with c1, c2, c3... variables
take a look at jsfiddle! isn't this what you want? i edited the post.
@jnick in that case you might want to use my example and just add more keys to your counter.
0

<body>
<button><a herf="#" onClick="myFunction('clk1','c1');">Para 1</a></button>
<p id="clk1"></p>

<button><a herf="#" onClick="myFunction('clk2','c2');">Para 2</a></button>
<p id="clk2"></p>

<button><a herf="#" onClick="myFunction('clk3','c3');">Para 3</a></button>
<p id="clk3"></p>

<script>
    var c = {c1:0, c2:0, c3:0};
    function myFunction(paragraph,varibl) {
            document.getElementById(paragraph).innerHTML = ++c[varibl];
    }
</script>
</body>

3 Comments

this doesn't do
@Moher what doesn't do, it seems to be changing the value.
i doesn't increment value for me though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.