0

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');
3
  • I'd say that there are three key things that this bit of code does. Which of them is giving you problems? You don't seem to have made any effort to solve this yourself. Commented Jul 2, 2015 at 8:05
  • @Quentin... my code works fine in jquery mode.. After built in jquery only came to know third party scripts are calling through javascript already. Commented Jul 2, 2015 at 8:29
  • youmightnotneedjquery.com Commented Jul 2, 2015 at 8:37

1 Answer 1

1

you could try this code:

edited:

document.getElementById(code).selected = 'selected';
document.getElementById('select').onchange.apply();
Sign up to request clarification or add additional context in comments.

14 Comments

Hi Sekretoz.. thanks for your comments.. I updated my question and code.. Have to my jquery code inside the javascript function. Whether your solution will work on this?
getting "Uncaught TypeError: document.getElementsByClassName(...).getElementById is not a function"
Yes.. it not changing.. It shows first element on the list as default. Also trigger event not working.
options[document.getElementById(code)]. the 'code' their must be the id of your option.ahm let me find another way to trigger the event.
any solution found pls? i tried different options.. but still no clue :(
|

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.