Alert using the select object [duplicate] - javascript

This question already has answers here:
Get selected value in dropdown list using JavaScript
(33 answers)
Closed 9 years ago.
I have tried using the select object with JavaScript but it dosent seem to be going. do anyone know what am talking about
this is what I have tried
var x = document.getElementById("mySelect").selectedIndex;
var u = document.getElementsByTagName("option")[x].value;
        document.getElementById("demo").innerHTML=u;

var sel = document.getElementById("mySelect"); //reference the select
var index = sel.selectedIndex; //find the index that was picked
var option = sel.options[index]; //select the option that was picked
document.getElementById("demo").innerHTML = option.value; //read the value
if you want the text and not the value use .text
document.getElementById("demo").innerHTML = option.text; //read the value
What this sample code does not do is deal if nothing is selected. That would be a selectedIndex equal to -1.

Related

Fade In random quote's from array [duplicate]

This question already has answers here:
CSS transition fade in
(4 answers)
Closed 3 years ago.
I need to get quotes to Fade In when clicking on the Get Quotes button. I am very new to JS and CSS and feel relatively lost. I also want to do this not using Jquery.
var quoteText = document.querySelector("h2");
var authorText = document.querySelector("h3")
var button = document.querySelector("button");
var body = document.querySelector("body");
button.addEventListener("click", function(){
var colorRandom = Math.floor(Math.random()*255)
var random = Math.floor(Math.random()*quotes.length)
quoteText.textContent = quotes[random];
authorText.textContent = "- " + authors[random];
})
First, if you want to get all (more than one) quotes you have to use querySelectorAll(), because they querySelector() method returns the first element. When you get all elements you can return random index in range of nodeList.length.
This is very simple example in https://codepen.io/iganchev87/pen/vqxjNm . If you want you can add in function with event listener and so on.
I hope it will help you.
var allQuotes = document.querySelectorAll("h2");
var randomIndex = Math.floor(Math.random()*(allQuotes.length));
console.log(allQuotes[randomIndex].textContent);

Pushing value to empty array [duplicate]

This question already has answers here:
How to append something to an array?
(30 answers)
Closed 5 years ago.
Trying to push the value I get from the onlick to a empty array and I cant seem to get it right.
var result = [];
$('#submitButton').click(function(){
var textField = $('#textField').val();
$('#result').append(textField, '<br/>');
$('#textField').val('');
result($(this).val());
});
Use this to push values in array
result.push($(this).val())
var result = [];
$('#submitButton').click(function(){
var textField = $('#textField').val();
$('#result').append(textField, '<br/>');
$('#textField').val('');
result.push($(this).val()); /* You have to use push method */
});
In your code, this refers to button(#submitButton), not to element of which you want to access value.
Use Array.prototype.push to push value in array
var result = [];
$('#submitButton').click(function() {
var textField = $('#textField').val();
$('#result').append(textField, '<br/>');
$('#textField').val('');
result.push(textField);
});

Javascript function to return a variable [duplicate]

This question already has answers here:
Is there a way to access a javascript variable using a string that contains the name of the variable?
(4 answers)
Closed 8 years ago.
I am trying to use a drop down box to update some data. what I would like to do is use a function to return the variable ds,ds1 depending on what the user selects. For example. if they select ds from the dropdown box I want the function to return the actual var ds and not the ds value, since I want to reference the variable ds in another bit of code depending on the selection. Hope this makes sense.
Thanks for your help,
<div >
<select id="dropdown" onchange="updateData()">
<option value="ds">ds</option>
<option value="ds1">ds1</option>
</select>
</div>
var ds = [[{x:0,y:72}],[{x:0,y:28}]];
var ds1 = [[{x:0,y:2}],[{x:0,y:50}]];
function myVar() {
return (dropdown.value);} //I want this to be the variable ds i.e. var ds //[[{x:0,y:72}], [{x:0,y:28}]]; not the value.
var arr = {};
arr["ds"] = [[{x:0,y:72}],[{x:0,y:28}]];
arr["ds1"] = [[{x:0,y:2}],[{x:0,y:50}]];
function myVar() {
return (arr["dropdown.value"]);
}

Get the option value in select in javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get the selected value of dropdownlist using JavaScript?
How to get the value of a selected text in javascript
<select id="short_code">
<option value="12">First</option>
<option value="11">Second</option>
<option value="10">Third</option>
<option value="9">Fourth</option>
</select>
I need to do this:
if(document.getElementById("short_code").options.item(document.getElementById("short_code").selectedIndex).text)== "First")
//get the value of the option Fist , how to get the value?
var elem = document.getElementById("short_code"),
selectedNode = elem.options[elem.selectedIndex];
if ( selectedNode.value === "First" ) { //...
How about this:
var select = document.getElementById('short_code');
var options = select.options;
var selected = select.options[select.selectedIndex];
//which means that:
console.log(selected.value || selected.getAttribute('value'));//should log "12"
console.log(selected.innerHTML);//should log(First);
Loop through all the options (either creating a reference variable, like I did with options, or plain for (var i=0;i<select.options.length;i++)) and you can get all info you need

How get the text from a listbox item? [duplicate]

This question already has answers here:
How to get Javascript Select box's selected text
(7 answers)
Closed 3 years ago.
When I try to get the text from a Listbox item in javascript it always returns undefined.
winkeloptie.value = winkels[i][0];
winkeloptie.text = (winkels[i][0]);
alert(winkeloptie.value) ///This returns the value very nicely
alert(winkeloptie.text) //Returns undefined
How can I get the text?
edit: for more info:
function addWinkels(){
var winkellijst = document.getElementById('winkel');
for (var i = 0;i < winkels.length;i++){
var winkeloptie = document.createElement("Option");
winkeloptie.text = (winkels[i][0]);
winkeloptie.title = (winkels[i][1]);
winkeloptie.value = winkels[i][0];
winkellijst.options.add(winkeloptie);
}
}
var x=document.getElementById("mySelect").selectedIndex;
var y=document.getElementById("mySelect").options;
alert("Index: " + y[x].index + " is " + y[x].text);

Categories

Resources