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

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);

Related

Using id as variable in querySelectorAll() [duplicate]

This question already has answers here:
How do I concatenate a string with a variable?
(5 answers)
Closed 4 years ago.
I need to use an id as variable in querySelectorAll() as shown below.
var thismodal = "myModal";
var clicked_id,trueModalId;
function openModal(clicked_id) {
trueModalId = thismodal + clicked_id;
document.getElementById(trueModalId).style.display = "block";
// var thisSlides = "mySlides" + clicked_id;
if($('trueModalId').find('.modal-content').length) {
alert(trueModalId);
}
}
function showSlides(){
var slides = document.querySelectorAll('trueModalId >.modal-content > .mySlides');
}
How do I make this work?
Just like every variable and string concatenation in js works. You forgot the # as an indicator for id though
var trueModalId = "myModal";
var slides = document.querySelectorAll('#' + trueModalId + ' >.modal-content > .mySlides');

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);
});

Pass list of json values in an array using javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I'm trying to pass a list of json Object in an array using javascript.
Here is the list:
[{"_id":1,"json":{"age":10,"name":"carlos"}},{"_id":2,"json":{"age":10,"name":"carlos"}}]
Here is the code:
var arrayResults = '{"files":' + '[{"_id":1,"json":{"age":10,"name":"carlos"}},{"_id":2,"json":{"age":10,"name":"carlos"}}]}';
alert (arrayResults.files.length);
var jsonData = JSON.parse(arrayResults);
for (var i = 0; i < arrayResults.files.length; i++) {
var file = jsonData.files.age[i];
alert(file);
}
Can I have some help please? Thanks
It's not exactly clear what you are looking for, but the following script does what I think you want, which is to iterate through the pieces of your data and alert the interesting facts about them:
var arrayResults = '{"files":' + '[{"_id":1,"json":{"age":10,"name":"carlos"}},{"_id":2,"json":{"age":10,"name":"carlos"}}]}';
var jsonData = JSON.parse(arrayResults);
alert (jsonData.files.length);
for (var i = 0; i < jsonData.files.length; i++) {
var file = jsonData.files[i];
alert(file.json.name + " " + file._id + " is " + file.json.age);
}
There were a number of things wrong with your posted code; rather than enumerating all of the problems, perhaps you can glean from this answer enough to help you continue your work.
var arrayResults = '{"files":' + '[{"_id":1,"json":{"age":10,"name":"carlos"}},{"_id":2,"json":{"age":10,"name":"carlos"}}]}';
//alert (arrayResults.files.length);
var jsonData = JSON.parse(arrayResults);
alert (jsonData.files.length);
for (var i = 0; i < jsonData.files.length; i++) {
var file = jsonData.files[i];
alert(file.json.age);
//Don't know where counter_name is defined
//alert(file.counter_name);
}

Alert using the select object [duplicate]

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.

JavaScript - Using a variable to refer to a property name? [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 8 years ago.
I am creating my own object:
gridObject = new Object();
I am then using jquery to pull the contents of list item tags, which themselves are filled with tags that have specific class names:
<li row="1"><p class="department" rowitem="department">Photography</p>...</li>
I am pulling them using this code:
//make object from results
gridObject = new Object();
//get all the rows
var rowlist = $('li[row]');
for(var r=0; r<rowlist.length; r++) {
//make gridObject row element here
//get the row content
var thisrow = $(rowlist[r]).html();
//get all the p tags
var rowitems = $(thisrow + 'p.[rowitem]');
//get field name
for(var ri=0; ri<rowitems.length; ri++) {
if (r < 2) { //this is temporary just for testing
var fieldname = $(rowitems[ri]).attr('rowitem');
var fieldvalue = $(rowitems[ri]).html();
}
}
Ia m getting hung up passing this into my object. Two questions. Can an object property be made with a variable name, like so
griObject.fieldname = fieldvalue;
and can the objects have parent/child relationships such as:
gridObject.r.fieldname = fieldvalue;
in this case both r and fieldname would be variables. Or should I just be working associative arrays to achieve something similar?
This is in answer to a follow up question I posted below: "Is there a print_r equivalent in javascript" - you can use iterator, a bit more typing but does the trick:
//loop through search data
var it = Iterator(filteritems);
for(var pair in it) {
console.log("key:" + pair[0] + ", value:" + pair[1] + "\n");
}
If you want to use a variable property name, use subscript syntax:
var fieldname = 'test';
//These two lines are equivalent as long as fieldname is 'test':
gridObject[fieldname] = fieldvalue;
gridObject.test = fieldvalue

Categories

Resources