Attribute value as variable name [duplicate] - javascript

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 7 years ago.
I'm trying to get a dynamic attribute value to be used as a var name
What I have:
$(this).html(""+$(this).attr("id")+"");
It gives me the value as plain text. What I want is that it uses the attr("id") value as a Var like:
$(this).html(""+myVar+"")
Final example:
<div class="text" id="text1"></div>
<div class="text" id="text2"></div>
var text1 = "hello world"
var text2 = "bye world"
$(document).ready(function () {
$(".text").click(function(){
$(this).html(""+$(this).attr("id")+"");
});
});
Basically I want a dynamic way of calling to certain id's and getting their attribute value to be used as the Var name instead of plain text.

It all depends on variable namespace and scope, but generally next code should do the job.
$(".text").click(function(){
var varFromAttr = window[$(this).attr("id")];
$(this).html(varFromAttr);
});
Question is almost the same as this question.

Related

Store [value] contents from browser element as a string [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Get value of hidden input, send to another hidden input
(3 answers)
Closed 4 years ago.
I have a quick question regarding being able to store parts of an element as a string which I can later recall.
Example:
I have an element that looks like this
<input type= "hidden" name="Posted" id="Posted" value="100|307|151|16">
I can retrieve the element with document.getElementById('#Posted') and now I want to be able to take the contents in the [value] tag and store them as a string in a new variable, so I can get something like this:
var inputValue = "100|307|151|16"
Which I can later recall in my code.
Maybe I'm blind but after a bit of searching I've still come up with nothing. Thanks in advance for the help!
There's two different approaches you can take here.
You can either get the value of the hidden input, or you can get the value of the "value" attribute on the hidden input.
Here's how to do both:
var element = document.getElementById('Posted');
var value = element.value;
var attribute = element.getAttribute("value");
console.log(`value: ${value}`);
console.log(`attribute: ${attribute}`);
And here's a JSFiddle of that in action.
//If all you care about is the value, you can just use .value to get the information you want
const inputValue = document.getElementById('#Posted').value;
Have you tried:
var inputValue = document.getElementById('#Posted').getAttribute("value");

Issue getting my objects properties [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 4 years ago.
Hi guys I have just started learning to code and I have hit a road block in accessing the properties of the object I have created.
Here is my object.
var restaurantOrder = {
"my entree": "cheeseburger",
"my side": "fries",
"the drink": "water"
};
I would like to get the value of entree however nothing I am trying seems to work :(
Here is what I have tried.
var entreeValue = restaurantOrder.my entree;
var entreeValue = restaurantOrder[my entree];
var entreeValue = restaurantOrder.[my entree];
var entreeValue = restaurantOrder.["my entree"];
None of the above lines work :( Thank you for your help.
Because the properties of your restaurantOrder object have spaces, you will need to use [] to access them. You cannot use . like you can with a property name that is a single word.
Also, because of the spaces you will need to enclose the property name with quotes, so either:
var entreeValue = restaurantOrder["my entree"];
or
var entreeValue = restaurantOrder['my entree'];
will work.

how to get value by class name using javascript [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 7 years ago.
Sorry, it's basic one but I trying to search on google anyways but still not get success.
I want get value of this
<input type='hidden' class='hid_id' value='1' />
Using Javascript I want alert value 1
I trying this
var id = document.getElementsByClassName("hid_id");
alert (id);
But it's alert [object HTMLInputElement]
please help me now.
getElementsByClassName() returns an array so you have to access first element (if there is any). Then try accessing value property:
var id = document.getElementsByClassName("hid_id");
if (id.length > 0) {
alert (id[0].value);
}
jsfiddle
try this:
var id = document.getElementsByClassName("hid_id")[0].value;

How to take a value from a link using jquery [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 7 years ago.
I want to pass one value through ajax by taking the values from jQuery. But I am using link so I have problems taking the value. I tried the following,
<a id="addpa" class="ActionPopup" href="http://localhost:49951/admin/assignhome/Add?sPId=7">Add</a>
Jquery Code:
var spId = $("#addpa").prop("href"); // Here i am getting a whole Url
var thequerystring = getParameterByName("sPId");
The result is showing undefined. How to take the value of sPId? Give me ideas..
How to take the value of sPId?
Try using String.prototype.split() , Array.prototype.pop()
var spId = $("#addpa").prop("href").split(/=/).pop();

how to view the answer of a js function in a html input? [duplicate]

This question already has answers here:
Set the value of an input field
(17 answers)
Closed 7 years ago.
I've always been wondering this, but all websites seem to be answering the opposite (how to make text typed in an input be the input of a function).
Is there a way to do it?
Is this what you want to do?
function myFunction() {
// some functionality here
// this function must return something
}
var input = document.querySelector('input');
input.value = myFunction();
This will set the value of the input element to the value returned by myFunction.

Categories

Resources