TextChange while updating input fields [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have two text Fields
I have two fields, which is reference and hotel id as stated on the image,
the value (valeur) is editable and the key (nom) is fixed, what I need to do is the above input field of parameters in JSON will be updated each time the below values (valeur) is changed
so input Parameters will get update just the values in JSON format which are modified below.
The input field parameters will be saved in the DB as JSON format like in the image

Maintain an object and on an event (say a submit button), pick the values of the textboxes and add to the object. Use JSON.stringify to add that to the hidden textbox.
Demo: http://jsfiddle.net/abhitalks/2hv2K/1/
var obj = {}; // maintain an object
$("#btn").on("click", function() { // on an event, say a button click
obj[$("#name").val()] = $("#value").val(); // add the values to the object
$("#result").val(JSON.stringify(obj)); // stringify and put in the hidden field
});
Edit (based on Op's comment):
If you want to update the object itself, change the handler accordingly:
Demo 2: http://jsfiddle.net/abhitalks/C63tW/2/
var obj = {};
$("input#name, input#value").on("change", function() { // handle change event
obj["reference"] = $("#name").val(); // update reference property
obj["hotel-id"] = $("#value").val(); // update hotel-id property
$("#result").val(JSON.stringify(obj)); // update the result field
});

Related

Add listener and value to multiple types of elements on the page [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have multiple elements on the page including checkbox and textfield. I want to set their default value and add listener to them all. If they are a textfield, I want to add a input listener... if they are a checkbox, I want to add a change listener. This is what I have so far.
const settings = {
'setting1': 2,
'setting2': true,
'setting3': 400,
'setting4': true,
}
for (var prop in settings) {
console.log(prop)
}
but it doesn't iterate over the object. Any help on this will be appreciated.
//You need to do something like this
const inputs = document.querySelectorAll('input[type="text"]');
const checkboxs = document.querySelectorAll('input[type="checkbox"]');
for (input of inputs) {
input.setAttribute('value', defaultValue);
input.addEventListener('input', callbackFunction);
}
// just repeat the loop for the checkbox elements

How to get a child key value in Firebase Realtime Database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm new with Firebase and I'm currently using JavaScript SDK to integrate my web application with Firebase.
My question is: How to get value within the red box that I drew on the image?
I am using
snapshot.key
to access the key valuebut it returns the generated key (blue box) to me which is not the one I wanted.
Based on your comments, it is not 100% clear what you are looking for. If you know that you have a sub-node with the key speechtext under the recording parent node with key LRC2o.... you don't need to query for getting the key of this sub-node (since you know it).
If, on the other hand, you want to iterate on all the keys of the sub-nodes of the recording node, do as follows (based on your code):
var dbRecording = firebase.database().ref("recordings/");
dbRecording.once("value", function(snapshot2) {
if (snapshot2.exists()) {
snapshot2.forEach(function(value) {
var childObject = value.val();
Object.keys(childObject).forEach(e => console.log(`key = ${e}`));
});
}
});
If you want the keys and the values, do as follows:
var dbRecording = firebase.database().ref("recordings/");
dbRecording.once("value", function(snapshot2) {
if (snapshot2.exists()) {
snapshot2.forEach(function(value) {
var childObject = value.val();
Object.keys(childObject).forEach(e => console.log(`key = ${e} value = ${childObject[e]}`));
});
}
});

Getting data from HTML or JavaScript object? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Let's say I have a HTML table with 5k rows and 50 columns (generated from JavaScript object) and I would like to send 50 checked rows (checkbox) from client to server using HTTP (JSON). What would be more efficient? Iterating in HTML to find the checked rows or iterating trough my JavaScript object to find corresponding rows?
fields = columns (50)
values = rows (~5k)
JavaScript data object:
parent {
child: [{field1: value1, field2: value2, field3: value3, and so on...}]
}
I'm not sure what you are trying to do with this information, but interacting with the DOM is one of the slowest things you can do, so you should check the JavaScript objects.
While you generate each row, you keep a reference of the checkbox and you bind it to your data in a javascript object.
Then you add an event listener on the checkboxes: when you tick on or off a row, you push or remove the mapped data line in an array that will always be up to date and ready to be sent.

Set Different JS variable from PHP foreach loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have very annoying problem, I need to display MySQL queries by using a PHP foreach loop but because of this when i try to set a javascript value for each of the items that are looped through it only selects either the first or last value it ignores the rest.
Here's what i've tried so far:
I have given an input a class and given it a value as well, this is within the for-each loop.
<input class="classs" value="{{ $item->id }}"/>
Next i have tried to access this from a js function, which is outside of the for-each loop.
function addSubTaskToDatabase(){
var itemId = $('.class').val();
alert(itemId);
}
Problem is all this does is alert the last value in the for-each loop with that class, its the same for name and for id also.
Anyone know the fix..? all help is appreciated.
var itemId = $('.class').val();
is going to select all elements on the page with that class.
You probably want a click function on the elements, and use this to access the currently-clicked item:
$('.class').click(function() {
var itemId = $(this).val();
alert(itemId);
});
To get all values you need an $().each loop
$('.class').each(function() {
var itemId = $(this).val();
alert(itemId);
}
see https://api.jquery.com/each/

Retrieve value from input object plain js [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have the following field,
<input id="department" value="finance"/>
I'm trying to create an object variable containing the field in plain old javascript like so.
var $field = $(document.getElementById("department"));
I'm then trying to pull the value from the field variable, however it's failing.
$field.value;
Does anybody know what I'm doing wrong?
That looks like a jQuery object, which is a collection of DOM elements, and the value property is attached to the DOM elements.
You should have:
var field = document.getElementById("department");
// now you can access field.value
jQuery objects have a val() method that gets you the value of that property:
var field = $('#department');
// here you can call field.val();
Lose the call to $() if you want a DOM element, and not a jQuery object:
$field = document.getElementById("department");

Categories

Resources