This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 2 years ago.
I´m getting a NaN out of this script, is it a data type problem and how do i solve it?
<script>
function calc(){
var profile = document.getElementById("profil");
var hours = document.getElementById("hour");
document.getElementById("result").innerHTML=profile*hours;
}
</script>
Present the result like this
<button onclick="calc()">Calc</button><br>
<p id="result"></p>
You have to get the value from the element.
var profile = document.getElementById("profil").value;
With .getElementById you get a DOM Element.
Supposing "profil" and "hour" are input elements, you want to obtain their values:
<script>
function calc(){
var profile = document.getElementById("profil").value;
var hours = document.getElementById("hour").value;
document.getElementById("result").innerHTML=profile*hours;
}
</script>
Related
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");
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.
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();
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.
This question already has answers here:
How to select JSF components using jQuery?
(4 answers)
Closed 7 years ago.
I am trying to get input value after keypress by jQuery. I have tried few ways to get it and I receive undefined or "". Using JSF+RichFaces. Input is the <h:inputTextarea> Trying to get value by:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#myform:idTextarea").keyup(function(){
var testValue = jQuery("myform:idTextarea").val();
console.log(testValue);
});
});
</script>
The variable in log is undefined.
When I add "input:" like that
var testValue = jQuery("input:myform:idTextarea").val();
I get j_id12.
When change like that:
var testValue = jQuery("#myform:idTextarea").val();
I get "".
Have someone any clues?
You need to escape CSS meta-character while buidling the jquery selector:
jQuery("input\\:myform\\:idTextarea").val();
Escaping css meta character in jquery
You don't need to find same element for which keyup is registered, just use this
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#myform:idTextarea").keyup(function(){
var testValue = jQuery(this).val();
console.log(testValue);
});
});
</script>