I'm grabbing a the last transaction value from https://api.bitcoinaverage.com/ticker/global/CAD/ and want to update it into an input textbox, but no matter what I try it won't show up.
here's a fiddle
https://jsfiddle.net/h9w5tj8m/2/
var xbtc = new XMLHttpRequest();
xbtc.open('GET', 'https://api.bitcoinaverage.com/ticker/global/CAD/', true);
xbtc.onreadystatechange = function() {
if (xbtc.readyState == 4) {
var ticker = JSON.parse(xbtc.responseText);
price = ticker.last;
document.getElementById('btc').innerHTML = price;
document.getElementById('cad').innerHTML = price;
}
};
xbtc.send();
updated fiddle
use
document.getElementById('cad').value
instead of
document.getElementById('cad').innerHTML
Simply understand with user interacted element input,select,textarea all are call with value for get the data from the elements.
All Other element's are call with innerHTML for get the data from the element
also refer different between .value and .innerHTML
var xbtc = new XMLHttpRequest();
xbtc.open('GET', 'https://api.bitcoinaverage.com/ticker/global/CAD/', true);
xbtc.onreadystatechange = function() {
if (xbtc.readyState == 4) {
var ticker = JSON.parse(xbtc.responseText);
price = ticker.last;
document.getElementById('btc').innerHTML = price;
document.getElementById('cad').value = price;
}
};
xbtc.send();
There is no input type of "textbox". You need to change it to "text" or use a <textarea></textarea>. You can set the value of text input with .value and a textarea with .innerHTML or .value.
This fiddle works fine: https://jsfiddle.net/h9w5tj8m/4/
using value attribute will place the value u retrieved into a particular textbox having a specific id .For Example :
<input type ="textbox " id="a">
</input>
<script>
document.getElmentById("a").value=x;
</script>
//x is the variable where the retrieved data is stored.
try this
document.getElementById('cad').value = price;
Related
I want to extract the value from date field, but it doesn't work. I add the input this way:
var question0 = "<div id='0'><p>Please, enter the date: </p><br>"
+ "<input type=\"date\" id=\'contractdate\'></input><br></div>";
Here is how I tried to receive the value:
var text_Contract_Date = document.getElementById('contractdate').value;
//tried the code below, but didn't work
// var text_Contract_Date = document.getElementById('contractdate').valueAsDate;
// var text_Contract_Date = new Date(document.getElementById('contractdate').valueAsDate);
So, I want to get the value from input as a string, using pure JavaScript, because then it will be used to fill in the document.
Try this
var dateEntered = new Date(text_Contract_Date);
There is not enough information to resolve the issue, so I can only guess that you are probably inserting your variable into the DOM the wrong way.
If I call document.body.append(question0), only text is shown and not the tags.
Try moving content of question0 variable to your html file, then add onchange handler to your input, and also modify your .js file like below
function handleChange(event){
// here you can do whatever you want with the value of the input
alert(event.target.value)
}
<input type="date" id='contractdate' onchange="handleChange(event)"></input>
If you desperately want to create your HTML inside of Javascript, you have to do this like this:
// create div and assign id to it
const myDiv = document.createElement("div")
myDiv.id = '0'
// create p and set its contents
const myP = document.createElement("p")
p.textContent = "Please, enter the date: "
// create input, assign id to it and set its type to date
const myInput = document.createElement("input")
myInput.id = 'contractdate'
myInput.type = "date"
// put everything in your document
myDiv.appendChild(myP)
myDiv.appendChild(myInput)
document.body.appendChild(myDiv)
First example:
var dbSelected = "File selected: ";
var filenamePanel = document.getElementById('filenamePanel');
filenamePanel.textContent = dbSelected + files[0].name;
var postLink = files[0].link;
document.getElementById('postLink').value = postLink;
var postName = files[0].name;
document.getElementById('postName').value = postName;
If I use <input type="hidden" name="postName" id="postName"> to send the value to another page via POST with PHP, it works.
Second example:
function onSuccessCallback(Blob){
document.getElementById('postName').textContent = Blob.filename;
document.getElementById('postLink').textContent = Blob.url;
document.getElementById('results').textContent = JSON.stringify(Blob);
};
Now, If I use <input type="hidden" name="postName" id="postName"> on the second example to send the 'postName' id value to another page, the value is empty.
What changes are necessary, on the second example, so that I can send the 'postName' id value to another page using a hidden <input> field?
Use .value instead of .textContent to store the data in the hidden input field:
document.getElementById('postName').value= Blob.filename;
I am new to Jquery and Javascript. I've only done the intros for codeacademy and I have what I remembered from my python days.
I saw this tutorial:
http://www.codecademy.com/courses/a-simple-counter/0/1
I completed the tutorial and thought: "I should learn how to do this with Jquery".
So I've been trying to use what I understand to do so. My issue is that I don't know how to pass an argument for a variable from HTML to Jquery(javascript).
Here is my code:
HTML
<body>
<label for="qty">Quantity</label>
<input id="qty" type = "number" value = 0 />
<button class = "botton">-1</button>
<button class = "botton">+1</button>
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="test.js"></script>
</body>
Jquery/Javascript:
//create a function that adds or subtracts based on the button pressed
function modify_qty(x) {
//on click add or subtract
$('.botton').click(function(){
//get the value of input field id-'qty'
var qty = $('#qty').val();
var new_qty = qty + x;
//i don't want to go below 0
if (new_qty < 0) {
new_qty = 0;
}
//put new value into input box id-'qty'
$('#qty').html(new_qty)
})
};
$(document).ready(modify_qty);
How do I pass an argument of 1 or -1 to the function? I was using onClick() but that seemed redundant because of the $('.botton').click(function(){}).
Thank you
If you use data attributes on your buttons you can get the value you want.
HTML:
<button class = "botton" data-value="-1">-1</button>
<button class = "botton" data-value="1">+1</button>
JS:
function modify_qty() {
//on click add or subtract
$('.botton').click(function(){
//get the value of input field id-'qty'
var qty = parseInt($('#qty').val());
var new_qty = qty + parseInt($(this).data('value'));
//i don't want to go below 0
if (new_qty < 0) {
new_qty = 0;
}
//put new value into input box id-'qty'
$('#qty').val(new_qty)
})
};
$(document).ready(modify_qty);
More compact JS:
$(function() {
//on click add or subtract
$('.botton').click(function(){
//get the value of input field id-'qty'
var $qty = $('#qty'),
currentValue = parseInt($qty.val());
$qty.val(Math.max(0, currentValue + parseInt($(this).data('value'))));
})
});
Update:
Realized you could do this without the data attributes if want to since your button text is the same as your value.
$(function() {
//on click add or subtract
$('.botton').click(function(){
//get the value of input field id-'qty'
var $qty = $('#qty'),
currentValue = parseInt($qty.val()),
newValue = currentValue + parseInt($(this).text());
$qty.val(Math.max(0, newValue));
})
});
Here's a fiddle to help you grasp the what's going on. Basically, the reference to the element that triggered the event is $(this) or event.target. Things get a bit more complicated with self refence depending on the context you are in, however for $('selector').on('event',function(event){ console.log($(this)) //is the reference to $('selector') });. .attr() -> list of the element's attributes.
I m trying to assign cookie's value to the input field.
code
Javascript
if ($.cookie("clientcookie")) {
var cookieval = $.cookie("clientcookie");
var inputs = document.getElementById("search");
inputs.value =cookieval;
}
HTML
<input id="search" type="search" placeholder="Search Text" class="input-medium search-query" >
But its not working its says:
Use of attributes' specified attribute is deprecated. It always returns true.
inputs.value =cookieval;
and
TypeError: inputs is null
I also tried
$('#search').val(cookieval);
But not working.
How to assign value to the input field
try{
$.cookie("clientcookie",'test'); //setting cookie in browser
if ($.cookie("clientcookie")) {
var cookieval = $.cookie("clientcookie");
var inputs = document.getElementById("search");
inputs.value =cookieval;
}
}catch(e){
alert(e);
}
very simple:
1. check cookie exist
2. if so then extract its value
3. fill text field's value.
check working example in jsfilddle.
I think if ($.cookie("clientcookie")) is not proper way to check the cookie.
Try this.
if ($.cookie("clientcookie") !== null) {
var cookieval = $.cookie("clientcookie");
var inputs = document.getElementById("search");
inputs.value =cookieval;
}
jsfiddle demo
I want to change the label value from '0' to 'thanks' in below label, on checkbox click event.
<input type="hidden" name="label206451" value="0" />
<label for="txt206451" class="swatch_text" >Chestnut Leather</label>
<input type="checkbox" name="field206451" class="swatch_check" id="txt206451" value="SELECTED"/>
The Javascript is as below.
var cb = document.getElementById('field206451');
var label = document.getElementById('label206451');
cb.addEventListener('click',function(evt){
if(cb.checked){
label.value='Thanks';
}else{
label.value='0';
}
},false);
But this is not working. Any idea?
very simple
$('#label-ID').text("label value which you want to set");
This will work in Chrome
// get your input
var input = document.getElementById('txt206451');
// get it's (first) label
var label = input.labels[0];
// change it's content
label.textContent = 'thanks'
But after looking, labels doesn't seem to be widely supported..
You can use querySelector
// get txt206451's (first) label
var label = document.querySelector('label[for="txt206451"]');
// change it's content
label.textContent = 'thanks'
You're taking name in document.getElementById() Your cb should be txt206451
(ID Attribute) not name attribute.
Or
You can have it by document.getElementsByName()
var cb = document.getElementsByName('field206451')[0]; // First one
OR
var cb = document.getElementById('txt206451');
And for setting values into hidden use document.getElementsByName() like following
var cb = document.getElementById('txt206451');
var label = document.getElementsByName('label206451')[0]; // Get the first one of index
console.log(label);
cb.addEventListener('change', function (evt) { // use change here. not neccessarily
if (this.checked) {
label.value = 'Thanks'
} else {
label.value = '0'
}
}, false);
Demo Fiddle
Try
use an id for hidden field and use id of checkbox in javascript.
and change the ClientIDMode="static" too
<input type="hidden" ClientIDMode="static" id="label1" name="label206451" value="0" />
<script type="text/javascript">
var cb = document.getElementById('txt206451');
var label = document.getElementById('label1');
cb.addEventListener('click',function(evt){
if(cb.checked){
label.value='Thanks'
}else{
label.value='0'
}
},false);
</script>
Based off your code, i created this Fiddle
You need to use
var cb = document.getElementsByName('field206451')[0];
var label = document.getElementsByName('label206451')[0];
if you want to use name attributes then you have to take the index since it is a list of items, not just a single one. Everything else worked good.
hope this help someone else :
use innerHTML for using label object.
document.getElementById('lableObject').innerHTML = res.FullName;