How I can use array with onchange in Javascript?

I want to use Array for multiple data in onchange element but is this an accurate coding?

For example:

<select id="Years" onchange="showContent('Years', ['rightPanelTxt', 'hrefDataAttr'])">

Javascript:

    function showContent($kaynak, $gosterilecekKonum) {
    
    	var $gosterilecekKonum = [];
    	for (var i = 0; i < $gosterilecekKonum.length; i++) {
    
    		var selected_secenek = document.getElementById($kaynak).value;
    		document.getElementById($gosterilecekKonum[i]).innerText = selected_secenek;
    		$($gosterilecekKonum[i]).attr('data-bid', selected_secenek);
    	};
    
    }

gosterilecekKonum This is array
Thank you

Have you tried this? It should just work.

Yes, not working. I’m not getting any error messages on console.

The problem is you are setting $gosterilecekKonum to an empty array as the very first line of showContent. Try removing that line and see if it works.

You talking about this line, right? var $gosterilecekKonum = []; If yes. I removed and Only worked for this element => rightPanelTxt

Since the function is working for one of them and not the other, I can’t tell exactly what is going wrong from that function then. Possibly you’ve misspelled hrefDataAttr either in your JS or in the HTML, or forgotten to add id="hrefDataAttr" to something?

1 Like

Thank you for your time :+1: