I'm having trouble trying to log the value of a input.
So my script is showing below.
document.getElementById("news").innerHTML = '<input type="text" id="myText" value="Mickey">';
var x = document.getElementById("news").value;
console.log(x)
I expected it to log "Mickey" to the console. Instead it logs "undefined"
Can someone help me understand why and possibly help me with a solution to my problem. Thank you.
You're trying to get the value from wrong element. Try using the input Id and you'll get results.
var x = document.getElementById("myText").value;
You need to get the value from input id--^
document.getElementById("news").innerHTML = '<input type="text" id="myText" value="Mickey">';
var x = document.getElementById("myText").value;
console.log(x)
<div id="news">
</div>
Related
I trying to get the value of an input type="text" and with it, check if it really exists at my database and, if true, append it with an input type="hidden" value="value-of-last-input", but my code isn't working.
Here:
<input type="text" class="form-control" id="codigo" name="codigo" onblur="RetirarProduto(this)" />
and JS:
function RetirarProduto(campo) {
var codigo = campo.value;
alert(codigo);
});
I used alert to check the value, but it is not working.
Your function has one extra parenthesis at the end. Change your JavaScript to:
function RetirarProduto(campo) {
var codigo = campo.value;
alert(codigo);
}
And this should work. Here is a working JSFiddle. Let me know if I can help any further :)
Try this to get the value in the input form field. Then you can pass the value to your function and do whatever you need with it. Put this inside your input element.
onblur="var c = this.value; alert(c);"
I have a strange problem: I am getting a undefined alert window by running this code. I looked up all the entrys here but I couldn't find a solution.
I've checked, there is no identical id except this one
running the php file the result of var_dump($user_id) shows the right value
this is why I don't understand why the value in javascript is undefined all the time...
HTML:
<form action="?atn=surveyCode" method="post" class="form mg_top">
<label id="entersID" for="usersID">Enter your prolific ID:</label>
<input id="usersID" value="" type='text' class="form-control talign_center input" name="usersID">
<button id="survey" class="btn btn-primary form-control">Go to questions</button>
</form>
JavaScript: every alert is returning "undefined". I never get in the else part. This is really strange. I've many parts like this in my code and they are working perfectly.
$("#survey").click(function(e){
var prolific = $("usersID").val();
alert(prolific);
if(prolific==undefined){
alert(prolific);
$("#entersID").addClass("error-msg");
$("#entersID").parent().addClass("has-error");
e.preventDefault();
}
else{
alert(prolific);
window.open("http://www.w3schools.com");
}
});
PHP:
private function surveyCode(){
$userID = $_POST["usersID"];
var_dump($userID); //shows a value
}
Please help me, maybe this is a silly bug but I can't find it....
Change the line into
var prolific = $("#usersID").val();
You have to add the "#" sign before the id selector like below :
var prolific = $("#usersID").val();
I've been tryna do this simple danged var assignment all night, now its too late >=[.
<SCRIPT LANGUAGE="JavaScript">
var P;
function save(f1)
{
P = parseFloat(document.getElementByID('loan1').value);
}
<form name = "form" action = "mortgagecalc.html" id="f1" onSubmit="check_form(form);">
<b>Loan Amount</b>
<input type = "text" name = "loan" size = "15" id="loan1" onchange="save(form)"><br>
</form>
and every single time I tab out of the loan field the console gives me:
Uncaught TypeError: undefined is not a function
for the line P = parse...
I tried everything!!I followed the examples perfectly. What is up with javascript variables never working for me??? UGHHHHHHH
you have a typo error
parseFloat(document.getElementByID('loan1').value);
should be
parseFloat(document.getElementById('loan1').value);
You just made a typo, it's getElementById, not getElementByID. What you wrote doesn't exist (=undefined) and thus will trigger an error that undefined is not a function.
I'm trying to add coordinates to a input text after trimming characters from each end of the string. But for some reason my trimming part isn't working. Can anyone help me out here? I'd really appreciate it.
Here's my code:
<input type="hidden" id="latlng" onload="getFinal()">
<script>
function getFinal(){
var input = document.getElementById('latlng').value;
consolelog(input);
var last = (input.length)-1;
var final = input.substring(1,last);
document.getElementById('final').value = final;
}
</script>
<input type='hidden' id='final'>
::: EDIT :::
I've tried simplifying things because the value that needs to be trimmed is in the rest of the code. But for some reason, when I use the code below, it still won't put the value in the text box.
var final = pos.substring(1,(pos.length)-1);
document.getElementById("final").value = final;
Now I'm getting the error: TypeError: Cannot set property 'value' of null at :4:44
OnLoad should not work for input. You better write as a simple javascript block
<input type="hidden" id="latlng" onload="getFinal()" />
<input type='hidden' id='final' />
<script>
var input = document.getElementById('latlng').value;
consolelog(input);
var last = (input.length)-1;
var final = input.substring(1,last);
document.getElementById('final').value = final;
</script>
script code
intext = document.getElementById("intext");
totalin = document.getElementById("totalin");
function myFunction()
{
var x = document.getElementById("totalin");
x.innerHTML = intext.toString();
}
in the above code am getting object HTMLInputElement in the label with "totalin" id and textbox of number type with id = intext
i am new to javascript and i saw many other answers on similar problems but either cudn't understand the answers or they didn't worked .
thanks in advance for help.
part of Html code is as follows if required
<label for="text">input the income in numericals</label>
<input type="number" name="text" id="intext">
Submit
<label id="totalin" for="totalin">Label:</label>
i would really appreciate any help i am really in need of solution.
You need to get the value from the element:
x.innerHTML = intext.value;
You're also missing a quote close:
<input type="number" name="text id="intext">
^ RIGHT THERE