Ik heb drie knoppen op mijn website, deze doen telkens min of meer hetzelfde met iets andere info. Ik denk dat eenvoudiger zou moeten kunnen, maar weet niet excact hoe. Enige hulp?


<button id="send_1">Buy 1 day</button>
<button id="send_30">Buy 30 days</button>
<button id="send_100000">Buy Lifetime</button>

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"
      integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ=="
      crossorigin="anonymous" referrerpolicy="no-referrer"></script>
      
      <script>
$("#send_1").click(function () {
  console.log("click");
  hive_keychain.requestCustomJson(
    null, 'sm_token_transfer', 'active', '{"to":"eclipsi","qty":95,"token":"DEC","type":"withdraw","app":"splinterviewer"}', '1 day',
    function (response) {
        console.log(response);
        console.log(response.data.username);
        console.log(response.result.id);

        $.post("purchase.php", { name : response.data.username, days : 1 });
    }
  );
});
   
   $("#send_30").click(function () {
  console.log("click");
  hive_keychain.requestCustomJson(
    null, 'sm_token_transfer', 'active', '{"to":"eclipsi","qty":1195,"token":"DEC","type":"withdraw","app":"splinterviewer"}', '30 days.',
    function (response) {
        console.log(response);
        console.log(response.data.username);
        console.log(response.result.id);

        $.post("purchase.php", { name : response.data.username, days : 30 });
    }
  );
});

   $("#send_100000").click(function () {
  console.log("click");
  hive_keychain.requestCustomJson(
    null, 'sm_token_transfer', 'active', '{"to":"eclipsi","qty":26995,"token":"DEC","type":"withdraw","app":"splinterviewer"}', 'Lifetime',
    function (response) {
        console.log(response);
        console.log(response.data.username);
        console.log(response.result.id);

        $.post("purchase.php", { name : response.data.username, days : 100000 });
    }
  );
});
</script>
    </body>
</html>
Gebruik functies.
Met wat zoeken en puzzelen :

<button id="send_1" onclick="clickbtn( this.id )">Buy 1 day</button>
<button id="send_30" onclick="clickbtn( this.id )">Buy 30 days</button>
<button id="send_100000" onclick="clickbtn( this.id )">Buy Lifetime</button>

<p id="result"></p>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<script>

var obj = {
	'send_1': {
		'qty': 95,
		'days': '1 day'
		},
	'send_30': {
		'qty': 1195,
		'days': '30 days'
		},
	'send_100000': {
		'qty': 26995,
		'days': 'Lifetime'
		}
	};

function clickbtn( id ) {
	document.getElementById('result').innerHTML = 'Aantal = ' + obj[id]['qty'] + '<br />' + 'Dagen = ' + obj[id]['days'];
	// verder verwerken
}

</script>


Bron van inspiratie :
https://stackoverflow.com/questions/4329092/multi-dimensional-associative-arrays-in-javascript

Reageren