Javascript function to return a variable [duplicate] - javascript

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

Related

How do I convert an array from the ViewBag to a JScript array using Razor Syntax? [duplicate]

This question already has answers here:
How to return Json object from MVC controller to view
(4 answers)
Closed 7 years ago.
I am trying to re-code the following razor syntax to be workable code and am stuck:
var ServiceIndex = #ViewBag.ServiceID ;
var ServiceName = #ViewBag.ServiceName ;
var ServiceNotes = #ViewBag.ServiceNotes ;
My problem is that right now the ViewBag for those 3 arrays is empty and so it pukes on the lone semicolon.
You need to translate it into JSON on a server-side.
<script>
var ServiceNotes = #(Html.Raw(JsonConvert.SerializeObject(ViewBag.ServiceNotes)));
var ServiceName = #(Html.Raw(JsonConvert.SerializeObject(ViewBag.ServiceName)));
var ServiceIndex = #(Html.Raw(JsonConvert.SerializeObject(ViewBag.ServiceIndex)));
</script>
Then access it directly in JS.
The empty array is correctly processed by JsonConvert, so, if it is empty you will get something like
var ServiceNotes = [];
For the convenience of debugging and avoiding nested function calls you can also split serializing and output.
#{
string serviceNotesJson = JsonConvert.SerializeObject(ViewBag.ServiceNotes);
string serviceNameJson = JsonConvert.SerializeObject(ViewBag.ServiceName);
string serviceIndexJson = JsonConvert.SerializeObject(ViewBag.ServiceIndex);
}
#section scripts
{
<script>
var ServiceNotes = #(Html.Raw(serviceNotesJson));
var ServiceName = #(Html.Raw(serviceNameJson));
var ServiceIndex = #(Html.Raw(serviceIndexJson));
</script>
}

Access items within an JS object, append to each DOM element in order [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
I have the following object which has been passed from PHP
var phpVars = {"embedCode0":"<iframe id="player_0"></iframe>",
"embedCode1":"<iframe id="player_1"></iframe>",
"embedCode2":"<iframe id="player_2"></iframe>",
};
My HTML contains a matching number of <div class="video-container"></div> elements. I want to append each of the array items to each div in order. So that "embedCode0" is appended to the first div, "embedCode1" is appended to the second div and so on.
My jQuery function looks like this:
function vids(){
var $video = $('div.video-container');
$video.each(function(index) {
var $iframe = phpVars.embedCode + index;
$(this).append($iframe);
});
}
The function returns NaN. I have attempted to convert the index to a string but have not been able to succeed.
How can I increment the "embedCode" string?
You should be trying this
var $iframe = phpVars["embedCode" + index];
instead of
var $iframe = phpVars.embedCode + index;
When you are saying phpVars.embedCode + index it tries to first evaluate phpVars.embedCode and after that concatenating index in that.

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.

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 to create dynamic two dimensional array in jquery or js [duplicate]

This question already has answers here:
How can I create a two dimensional array in JavaScript?
(56 answers)
Is it possible to create an empty multidimensional array in javascript/jquery?
(8 answers)
Closed 9 years ago.
I need to create global two dimensional array in jquery or javascript
My function is like this
<script>
var globalArray[0] = new Array();
function createArray(){
alert(globalArray[0]);
}
</script>
<div><input type='button' value='save' onclick='createArray();'> </div>
On click of that button I am getting this error "globalArray[0] is undefined"
How can I create global dynamic multi dimensional array.
if (!globalArray[index])
globalArray[index] = []; // init the array.
globalArray[index].push(name);
You have a typo with the dot:
$.("#uname").val();
Change to:
$("#uname").val();
What are you trying to do with this code?
Update: (The question was totally edited.)
Your code:
var globalArray[0] = new Array();
globalArray[0] is invalid variable name, you need first to declare the array:
var globalArray = []; // Array literal.
globalArray[0] = [] // The element at position 0 is new an array.
Intead of
if(loop == 0){
globalArray[index][0] = uname;
}else{
globalArray[index][loop++] = uname;
}
Use this
if(loop > 0){
globalArray[index][loop++] = uname;
}else{
globalArray[index][0] = uname;
}

Categories

Resources