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 6 years ago.
Improve this question
Is there a way to watch variable changes in Jquery/Javascript like it is the case in AngularJS ?
Angular uses a digest cycle to detect changes after you update a variable. For instance, when you click on an element, it will change variables in scopes, and will immediately trigger a look through to see what has changed. You can accomplish something similar using a simple interval function:
var _watch = null;
var test = null;
setInterval(function() {
if ( _watch !== test ) {
_watch = test;
console.log('Variable changed.', test);
}
}, 100);
To test this out, simply type test = "test" in the console, and you should see "Variable changed."
You can use obj.watch but it's only supported in Gecko (Basically firefox) for now.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/watch
Edit: For Chrome , Object.observe can be used.
Use a getter/setter method:
var log = ['test'];
var obj = {
get latest () {
if (log.length == 0) return undefined;
console.log('getting latest!'); // or trigger an event
return log[log.length - 1]
}
}
console.log (obj.latest); // Will return "test".
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Functions/get
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 7 years ago.
Improve this question
I have this function which produces the correct value when run, but I am having a hell of a time displaying the results.
Here is the JS which is calculated onChange in a form I am trying to display the resulting value elsewhere on the form. The alert displays the correct value but my id remains blank.
Thanks in advance for taking a look
function calculate_mtow1() {
togw_n = 0;
togw = $('#togw').val();
if (togw != '' && togw != 0 && togw != 'Nan') {
var togw = togw.replace(",", "");
togw_n = togw;
}
burn_n = 0;
burn = $('#burn').val();
if (burn != '' && burn !=0 && burn != 'Nan') {
var burn = burn.replace(",", "");
burn_n = burn;
}
var mtow1 = parseInt(togw_n) + parseInt(burn_n);
$('#mtow1').val(mtow1);
document.getElementById('mtow1');
alert(mtow1);
}
<td>TOW + Fuel Burn =<span id="mtow1"></span></td>
Your code is getting the element with getElementById but then not doing anything with it. You need to assign the result of getElementById to something, or call methods on it on the same line. If your goal is to put the value of mtow1 into your <span>, try doing this:
// Solution 1
var spanElement = document.getElementById("mtow1");
spanElement.innerHtml = mtow1;
Alternatively, perhaps you were trying to display the value of mtow1 by using this jQuery:
$('#mtow1').val(mtow1);
That doesn't do what you think it does. It changes the "value" attribute of the span to the value of mtow1, but that change isn't visible to the user. It's the same as writing this as your HTML:
<td>TOW + Fuel Burn =<span id="mtow1" value="valueofmtow1"></span></td>
If you want to use jQuery instead of the getElementById method I posted above, you could do this:
// Solution 2
$('#mtow1').html(mtow1);
You don't need to do both. Either solution will work on its own.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
I'm trying to lookup a field's value, if it equals '1', then put the value '1' in different field, if not put a '0'.
I'm not sure why this isn't working, can anyone help?
<input type="text" name="_1_1_33_1_id" value="" onchange="checkLineManager();">
<input class="valueeditable" type="text" name="_1_1_118_1" id="_1_1_118_1" value="" >
Javascript:
function checkLineManager() {
if (document.getElementsByName('_1_1_33_1_id').value == '1') {
document.getElementById('_1_1_118_1').value = '1';
} else {
document.getElementById('_1_1_118_1').value = '0';
}
}
http://jsfiddle.net/nbren007/o9xp0efy/
Note the plural use of "elements" in the following line:
if (document.getElementsByName('_1_1_33_1_id').value == '1') {
This doesn't return an element, it returns a node list.
// To confirm that
alert(document.getElementsByName('_1_1_33_1_id').toString());
So you need to use:
if (document.getElementsByName('_1_1_33_1_id')[0].value == '1') {
There are other ways of accessing the element as well. Most notably through the form element approach.
The hint is in the method: getElementsByName returns more than one element - it returns an array of matching elements.
You need to use array notation to select the element from the array.
Change:
document.getElementsByName('_1_1_33_1_id')
To:
document.getElementsByName('_1_1_33_1_id')[0]
if (document.getElementsByName('_1_1_33_1_id')[0].value == '1') {
document.getElementById('_1_1_118_1').value = '1';
} else {
document.getElementById('_1_1_118_1').value = '0';
}
Or even neater, use a ternary statement:
document.getElementById('_1_1_118_1').value =
document.getElementsByName('_1_1_33_1_id')[0].value == 1 ? '1' : '0';
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 5 years ago.
Improve this question
i have json values like this, i try to use for each to get the result one bye one, but it was not returning to me
"[{"id":6,"userid":13,"title":"business3","bussinessTypeid":1,"photurl":"","cntry":[{"id":"9","name":"Bahrain"}]},
{"id":7,"userid":13,"title":"business6 arabic","bussinessTypeid":1,"photurl":"","cntry":[{"id":"1","name":"Afghanistan"}]},
{"id":10,"userid":13,"title":"E-commerce","bussinessTypeid":1,"photurl":"","cntry":[{"id":"1","name":"Afghanistan"}]},
{"id":11,"userid":8,"title":"Auto phone parts","bussinessTypeid":1,"photurl":"","cntry":[{"id":"1","name":"Afghanistan"}]},
{"id":19,"userid":8,"title":"التجارة الإلكترونية","bussinessTypeid":1,"photurl":".jpg","cntry":[{"id":"9","name":"Bahrain"}]},
{"id":20,"userid":8,"title":"E-commerce -online shopping","bussinessTypeid":8,"photurl":".jpg","cntry":[{"id":"9","name":"Bahrain"}]},
{"id":21,"userid":13,"title":"My new Business","bussinessTypeid":6,"photurl":".jpg","cntry":[{"id":"9","name":"Bahrain"}]}]"
can anyone guide me to achive this
I think it's being treated as a string try to remove the ( " ) on the start and end of it something like:
var json = [{"id":6,"userid":13,"title":"business3","bussinessTypeid":1,"photurl":"","cntry":[{"id":"9","name":"Bahrain"}]},{"id":7,"userid":13,"title":"business6 arabic","bussinessTypeid":1,"photurl":"","cntry":[{"id":"1","name":"Afghanistan"}]},{"id":10,"userid":13,"title":"E-commerce","bussinessTypeid":1,"photurl":"","cntry":[{"id":"1","name":"Afghanistan"}]},{"id":11,"userid":8,"title":"Auto phone parts","bussinessTypeid":1,"photurl":"","cntry":[{"id":"1","name":"Afghanistan"}]},{"id":19,"userid":8,"title":"التجارة الإلكترونية","bussinessTypeid":1,"photurl":".jpg","cntry":[{"id":"9","name":"Bahrain"}]},{"id":20,"userid":8,"title":"E-commerce -online shopping","bussinessTypeid":8,"photurl":".jpg","cntry":[{"id":"9","name":"Bahrain"}]},{"id":21,"userid":13,"title":"My new Business","bussinessTypeid":6,"photurl":".jpg","cntry":[{"id":"9","name":"Bahrain"}]}];
Then do the iteration:
for(id in json){
alert(json[id]['id']);
}
and don't include any next line spaces are ok as long as they are separated by a comma (,)
Use parseJson()
Description: Takes a well-formed JSON string and returns the resulting
JavaScript object.
$.each(data, function(key, value){
console.log(value.id, value.title);
$.each(value.cntry, function(key2, value2){
console.log(value2.id, value2.name);
})
});
Try this with jquery
var data = jQuery.parseJSON(yourJsonInString); //or this parser below
var data = JSON.parse(yourJsonInString);
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 9 years ago.
Improve this question
I have a loop with the jquery .each function that is filtering through IDs with the suffix _prop_val I then get the prefix and use it for other operations as you can see in the code below. At the moment I am having to use an if statement to set the rlPrice. Is there a way to get it in the loop i.e. prefixPrice and then have it set as rlPrice afterwards
$('[id$="_prop_val"]').each(function(){
var prefix = this.id.slice(0,2);
if( $('#'+prefix+'_prop_val').val() != ""){
$('#'+prefix+'_prop_val').prop('disabled', false).trigger('chosen:updated');
$('#'+prefix+'_ct_row').show();
$('#'+prefix+'_deactivate_btn').show();
if(prefix == "rl"){
rlPrice = $('#rl option:selected').data("unit-price");
}
checkExtra(prefix);
}else{
$('#'+prefix+'_container').addClass('inactive');
$('#'+prefix+'_activate_btn').show();
}
});
You might find an object more useful here. Define it before the loop:
var price = {};
Then in your loop just assign the price to a property with the prefix as a key - no need to check for a condition:
price[prefix] = $('#rl option:selected').data("unit-price");
Then you can just access the price using the prefix:
var rlPrice = price['rl'];