Why this JavaScript code does not work? [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
This code should working, but I have no idea why it is not working at all.
HTML:
<p><input type="file" size="50"></p>
<p><input type="button" value="test" onclick="test()"></p>
JavaScript:
test = function() {
console.dir(document.querySelector('input[type="file"]').value);
var a = document.querySelector('input[type="file"]').vaule;
console.dir(a);
};
The first console.dir can successfully display the selected file filename
whereas I store it in var a is return undefined, whats happended?
fiddle: jsfiddle.net/eb5tuo7o

With the console log you're using .value but when you're storing it you've misspelled it as .vaule.
test = function() {
console.dir(document.querySelector('input[type="file"]').value);
var a = document.querySelector('input[type="file"]').value;
console.dir(a);
};

There is no such thing as "vaule"
You can access html elements value by using
document.querySelector('input[type="file"]').value;
And if you are using jquery it's even simpler
$("input[type=file]").val();

Related

How can I make this take the text they put in the input box and then put it into local storage [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
i don't understand why it doesn't send what they put in the input box to local storage
<body>
<input id="name">
<button onclick="bob()">
save
</button>
</body>
<script>
const person = document.getElementByid("name");
window.localStorage.setItem('user', JSON.stringify(person));
</script>
You need to use the value property to get what the user entered.
function bob() {
const person = document.getElementById("name");
window.localStorage.setItem('user', person.value);
}

How do I make an alert() that prints out the value of a variabile? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to make an alert, that prints the value of a variabile, here is my attempt.
var username = prompt("I'm LaunchBot, what's your name?");
var print = alert() ;
alert() is a function. It take a parameter between the parenthesis. So just insert your variable username between them:
var username = prompt("I'm LaunchBot, what's your name?");
alert(username) ;
As suggested in the comment alert() doesn't return anything so do not add a variable assignment before.

javascript variable not populated correctly [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Basically, the "rec" variable is correctly filled, but it's not happening with the "email" variable . Why?
This is my code:
<textarea id="q" name="q"></textarea>
<button onclick='alert(rec);'>Click</button>
<a id="email" href="#" target="_blank">Email</a>
<script>
var rec;
var email;
$("#q").keyup(function() {
rec = $('#q').val();
email = 'www.corriere.it/' + rec;
$('email').attr("href", email);
});
</script>
https://jsfiddle.net/wvsc93d4/3/
You're not using the selector correctly, try:
$('#email').attr("href",email);
You're missing the # selector:
$('#email').attr("href",email);

Javascript String.replace [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Can someone please explain why this code isn't working?
(it has been simplified for this example)
$(document).ready(function () {
var test = 'broken';
test = test.replace('broken','working');
console.log(test); // working
var field = $('[for="tournament_name"]').html();
console.log(field); // Tournament Name:
console.log(typeof field); // string
field = field.relpace(':',''); // Uncaught TypeError: undefined is not a function
});
I don't understand why it is saying replace() is undefined?
I did read through the docs, what am I missing here?
Maybe it's a typo:
relpace --> replace

How to add property to a global Javascript object inside a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am tying to add a property to a JS object inside a function. I can do it outside but not inside. Please explain. Sorry. I am missing something very basic here.
var newobj = {'prop1' : 12, 'prop2' : 25};
myfunc(newobj);
function myfunc(someobj) {
someobj.prop3 = 45;
}
This gives a syntax error.
Chances are something else is interfering because it works for me.
If you dump newobj before the function call you get:
{"prop1":12,"prop2":25}
And after the function call:
{"prop1":12,"prop2":25,"prop3":45}
As you can see, the new property has been added.
I would suggest either looking at what you have more closesly (make sure you're not copying the value and then passing it) or add some console.log call in your code as it goes through. You can also, in most of the browsers, use the debugger to step through the code to see where it may be fouled.

Categories

Resources