I have a checkbox on my webpage. Based on if the checkbox is checked the variable abc_test needs to be changed either to "wfOutput" or "_blank".
On initial load the variable has the value of "wfOutput" but for some reason the formTarget does not react to the changes based on the variable value. If I change the variable manually everything works and the formTarget receives the correct value.
<input id='runwindow' type='checkbox'>
var abc_test = "wfOutput";
$("#runwindow").change(function() {
if($(this).is(":checked")) {
abc_test = "_blank";
return;
}
abc_test = "wfOutput";
});
The abc_test variable is used in the following code example.
var ap = $("<div>").autoprompt(
{
wfdInfo:xmlInfo,
formTarget:abc_test,
}).autoprompt("instance");
Any ideas what I am doing wrong?
but for some reason the formTarget does not react to the changes based on the variable value.
When you do
var ap = $("<div>").autoprompt(
{
wfdInfo:xmlInfo,
formTarget:abc_test,
}).autoprompt("instance");
the value of abc_test is read and assigned to the formTarget property of the object created by the initializer. There is no ongoing connection between the object property and the variable afterward; changing abc_test later has no effect on the object property.
You'll need to call autoprompt to update the formTarget option from your change handler. Most plugins offer some kind of "update" method.
Related
I want an easy way to set a variable value equal to n when function(n) runs.
Hovedkort
Prosessor
Arbeidsminne
I have this piece of HTML code. And I want the loadProdType function to change the value of a variable prodCat according to loadProdType's value. I tried the code under with and witout localstorage.setItem(loadCat, j). The js document is linked and there are no linking errors cause other pieces of code runs as it should.
var loadCat = 0
function loadProdType(j){
loadCat = j}
How can I set loadCat's value equal to loadProdType's value/parameter.
Thanks in Advance.
Edit:
Unsure how to set local storage for this but what I want the code to do is the following:
Change the value of variable prodCat according to the value of the onclick function.
Redirect to: products.html
When in products.html I run a piece of code that uses the prodCat value to choose a spesific index in an array.
All in all I just need to be able to change prodCat on one page(to the value of a funcion or with a onclick) and have it saved on the next.
Last edit
I'll create different functions for each onclick and try that with local storage.
You need either ? in the URL or local storage.
?
In main document:
location.href = "products.html?prodType=" + loadCat;
In products.html:
var loadCat = new URL(document.URL).searchParams.get("prodType");
localstorage
In main document:
window.localStorage.setItem("prodType", loadCat);
In products.html:
var loadCat = window.localStorage.getItem("prodType");
I have a function which copies the values of a group of inputs to another group of inputs if the user clicks a button.
The function works fine but I'm having trouble with the vars I'm using to get the information. I want to use global variables because I want to use the same variables later on but it only works when I wrap those vars inside the function.
I've posted the two scenarios where it's working and not working below. Can anyone explain why I cannot access the correct value at that given time using a global variable?
EDITS: The 3 elements #admin-name, #admin-email and #admin-number are all present in the DOM when the script is called, as I am doing everything with document ready. I understand that when the script first runs these values will be blank because they haven't been filled out by the user yet. What I don't understand is why can't jQuery get the value once it has been filled out and I call the variable on the click function.
Not Working
var contactName = $('#contact-name').val();
var contactEmail = $('#contact-email').val();
var contactNumber = $('#contact-number').val();
$(".step-two .copy-details").on('click', function(){
$('#admin-name').val(contactName);
$('#admin-email').val(contactEmail);
$('#admin-number').val(contactNumber);
});
Working
$(".step-two .copy-details").on('click', function(){
var contactName = $('#contact-name').val();
var contactEmail = $('#contact-email').val();
var contactNumber = $('#contact-number').val();
$('#admin-name').val(contactName);
$('#admin-email').val(contactEmail);
$('#admin-number').val(contactNumber);
});
Man I struggled with this one, this post helped flesh it out for me, jQuery Global Variable. The problem is the variable called in the click function was still getting the original value of 0. To make the variable update when a new value is added you need to declare it and then wrap it in a change function like so:
JS
// declare the variable
var contactName;
// wrap it in a change function so the value updates
$('#contact-name').on('change', function(){
contactName = $('#contact-name').val();
});
// use the variable inside the function and it will show the updated value
$('.step-two').on('click', 'button', function(){
$('#admin-name').val(contactName);
console.log('contactName = ' + contactName);
});
This is my fiddle. http://jsfiddle.net/aaScC/
Please check in the example, the Score property has 3.5 value but it is being displayed as 1. I know the score property is bound to dropdown value so its coming as 1. But i want 3.5 to be displayed. Please help.
var GoalsModel = function (goals) {
var self = this;
self.goals = ko.observableArray(ko.utils.arrayMap(goals, function (goal) { return new Goal(goal) }));
};
The problem is that you just make the select element invisible. You don't want the element at all. You can use bindings if or ifnot to control this.
Here is an updated example: http://jsfiddle.net/waxwing/aaScC/1/ . I wrapped the select inside a span to make it work, but you can also use virtual bindings if you don't like to change your DOM structure.
I have a global variable listId(refer the below code) declared with a default value and then I am assigning it inside the jq("#nav1 li a").click(function() But once any of the other javascript function call takes place after this one, the listId does not reflect the changed value, instead its just the default value assigned during declaration. How can I make it reflect the changed values?
thanks
listId = 'x';
var jq=jQuery.noConflict();// for avoiding conflict
jq("#nav1").click(function(){
alert(this.id);
listId = this.id; //this.id is displayed in alert message
});
function pageSwitch(){
alert('on change id : '+listId);
//when called after the click function, this does not reflect changed values
}
The code you supplied worked for me. The listId variable was successfully changed to "nav1". I've written up a little test script, which changes listId to the div's id onClick, and then uses a function to change it back to "x". It works every time. Click here to test it.
Thanks guyz for the help..actually the function was invoked on click of an h:commandLink..so each time the page got reloaded and so did the js functions...I just used the a4j:commandLink and it worked fine...I should have presented the full picture..Sorry for that..will take care next time.
is it possible to check whether default value (set using value="abcdef") of a field with id="someidset" have changed without having info about this default value? Hope it's kind of clear...
When you update the content of an element, the value property changes. However, the value attribute does not. This means that, presuming the value was defined in the value attribute in the original HTML, you can compare the two to see if the one has changed:
var el = document.getElementById('someidset');
if (el.value != el.getAttribute('value')) {
// value has changed
}
Note that this will only reliably work with type="text" inputs.
Well there are attributes and properties.
var someInput = document.getElementById('someInput');
someInput.value; // inputs value right now
someInput.getAttribute('value'); // inputs value set at start
Try this demo: http://jsfiddle.net/maniator/wVazC/
change the value right after the alert and wait 10 seconds
Sure, you can use the defaultValue property. It should work for most types of <input /> elements. Just check it against the value property.
Here's an example.