TDG
TDG

Reputation: 1302

jQuery to JavaScript code

I want to convert below jQuery code to pure JavaScript to load with JavaScript function.

HTML

<select name="select" id="select" >
    <option id="EN" value="global.html">Global</option>
    <option id="AU" value="australia.html">Australia</option>
    <option id="ID" value="Indonesia.html">Indonesia</option>
</select>

JS code:

function getCode () {
    var now = new Date().getTime();
    var randomNumber = Math.floor((Math.random() * 100) + 1);
    var uniqueNumber = now + 'a' + randomNumber;
    $.getScript("getCountryCode.js?" + uniqueNumber, function(data, textStatus, jqxhr) {
        if (country){
           var code = country;
           alert("Country code:" + code);
           document.getElementById(code).selected = 'selected';
           document.getElementById('select').onchange.apply();
         }
    });
}

Want to add below code in pure JavaScript format to fix the issue.

$('.selector #select option[id='+code+']').attr('selected', 'selected');
$('.selecto #select').trigger('change');

Upvotes: 0

Views: 83

Answers (1)

Sekretoz
Sekretoz

Reputation: 112

you could try this code:

edited:

document.getElementById(code).selected = 'selected';
document.getElementById('select').onchange.apply();

Upvotes: 1

Related Questions