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');
halfer
20.2k20 gold badges111 silver badges208 bronze badges
-
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.Quentin– Quentin2015年07月02日 08:05:53 +00:00Commented 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.TDG– TDG2015年07月02日 08:29:51 +00:00Commented Jul 2, 2015 at 8:29
-
youmightnotneedjquery.comgeckob– geckob2015年07月02日 08:37:21 +00:00Commented Jul 2, 2015 at 8:37
1 Answer 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
TDG
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?
TDG
getting "Uncaught TypeError: document.getElementsByClassName(...).getElementById is not a function"
TDG
Yes.. it not changing.. It shows first element on the list as default. Also trigger event not working.
Sekretoz
options[document.getElementById(code)]. the 'code' their must be the id of your option.ahm let me find another way to trigger the event.
TDG
any solution found pls? i tried different options.. but still no clue :(
|
lang-js