I have a problem accessing for loop variable inside my AJAX functions. Below is my code.
for (var x; x<=8; x++){
$('#sumbit_button'+x).bind('keyup paste', function(value){
var val = $(this).val();
$.post('index.php', {val : val }, function(data){
$('#sumbit_this_'+x).html(data);
verify_error(value);
});
});
}
The line
$('#sumbit_button'+x).bind('keyup paste', function(x){ ... }
Means that your previously defined variable x is overriden in local function scope by function argument x which is actually the keyup or paste event. If you want to keep reference to x, rename function(x){...} to something more logical, like function(event) {...} or do not use function argument at all if you don't need it function(){...}.
It would be a good idea to read more about how jQuery and Javascript event handlers work.
Update
Your current version of the script will not work either because you're still passing jQuery event object to the verify_error instead of x. The problem is that you do not pass x when declaring an event handler, you're declaring a new variable, which is currently called value. Delete the variable value from $('#sumbit_button'+x).bind('keyup paste', function(value){...} and simply pass 'x' to the verify_error as in the first version of your script.
You've overwritten the x by your function(x)
Try renaming x in function(x) to something else like function(y) or whatever that make sense.
for (var x; x<=8; x++){
$('#sumbit_button'+x).bind('keyup paste', function(){
var val = $(this).val();
$.post('index.php', {val : val }, function(data){
$('#sumbit_this_'+x).html(data);
verify_error(x);
});
});
}
Related
Is it possible to have the filebeat script processor call functions from the main event function?
The idea would be to call function X from the main event function if the field was an object or value was equal to 5 (ie some event happened and needed special processing). Therefore, function X would do something.
I found a solution, here's a snippet showing how it was done:
- script:
lang: javascript
id: get_fields
source: >
function check(event, field){
event.Append("fieldAppender", field);
}
function process(event) {
var a = event.Get();
for( var field in a){
check(event, field);
}
}
In my controller I've a statement assigning an empty function in a variable based on a condition.
var vm = this;
vm.emptyFunction = angular.isFunction(callback) ? callback : function() {};
How can I test that this vm.emptyFunction is actually an empty function?
Things which didn't work:
expect(vm.emptyFunction).toEqual({});
expect(vm.emptyFunction()).toEqual({});
expect(vm.emptyFunction).toEqual(() => {}); //using typescript
Thing which work is:
expect(vm.emptyFunction).toEqual(jasmine.any(Function));
But this will get valid for any function definition.
The only thing I can think of is to use the same reference to the same empty function.
var emptyFunction = function {};
and then use
vm.emptyFunction = angular.isFunction(callback) ? callback : emptyFunction;
Then test
expect(vm.emptyFunction).toEqual(emptyFunction);
My task is get children of some element, iterate through them, and using nightwatch assert make some tests. Here is example of code, what I need:
browser.url("http://someurl.com")
.getAttribute("#parent", "children", function (children) {
var assert = browser.assert;
var fisrtChild = children[0];
var secondChild = children[1];
assert.equal(fisrtChild.innerHTML, "Hello");
assert.equal(secondChild.innerHTML, "World");
})
So, is Nightwatch can do something like this?
P.S. I tried to use 'elements' command, but it returns something like this:
{ state: 'success',
sessionId: '63af98a9-d395-4e44-9529-e24a7ad7ff87',
hCode: 1223583237,
value: [],
class: 'org.openqa.selenium.remote.Response',
status: 0 }
Firstly "children" is NOT a valid attribute for HTML elements.
Secondly the callback function will simply pass the result of said command should you give it an argument. So the object displayed is in actual fact the result of the getAttribute command.
you could probably use the browser.execute command to allow you to do what you are trying to do
Essentially do something like the following: -
var firstChild;
var secondChild;
browser.execute(function() {
var childNodes = [];
childNodes.push(document.getElementById('parentID').childNodes);
firstChild = childNodes[0].innerHTML;
secondChild = childNodes[1].innerHTML;
}, [])
You will then be able to perform assertions on the values of firstChild and secondChild.
NOTE: Have not tested this and you may need to play around with it a little but hopefully you get the general idea
you can try below code. It will fetch all the buttons and then click on all sequentially.
browser
.url("http://someurl.com")
.elements('css selector', "button", function (links) {
for (var i = 0; i < links.value.length; i++) {
browser.elementIdClick(links.value[i].ELEMENT);
}
})
Let's say I have a map:
map = new can.Map({foo: 'bar'})
and I want to bind the value of foo in another map. I could do:
otherMap = new can.Map({fizzle: map.compute('foo')})
but this doesn't behave the way I would expect.
I would expect otherMap.attr('fizzle') to return bar, but instead it returns a function. I have to call otherMap.attr('fizzle')() instead.
I would expect to be able to change the value by calling otherMap.attr('fizzle', 'moo'), but this doesn't change the computed value. Instead, if I want to change the underlying value, I have to call otherMap.attr('fizzle')('moo').
Is there a way to create a map with computed values that behave like normal attributes?
Thanks!
I would recommend using the define plugin which makes it easy to create computed getters and setters without having to explicitly create computes. In your example like this:
var map = new can.Map({
foo: 'bar',
baz: 'bla'
});
var OtherMap = can.Map.extend({
define: {
fizzle: {
get: function() {
return map.attr('foo') + '/' + map.attr('baz');
},
set: function(value) {
map.attr('foo', value);
}
}
} });
var other = new OtherMap();
console.log(other.attr('fizzle'));
other.attr('fizzle', 'something');
console.log(map.attr('foo'));
Demo in this Fiddle.
I would like to overwrite the value of the "password" field before submiting a form on Jquery using AjaxSubmit function.
I know I can just update the value on the input field but, I don't want the user to see this transformation. In other words, I just want to send a custom value to the password field and keep the current value on the screen...
How could I do that?
My current code:
var loginoptions = {
success: mySuccessFuction,
dataType: 'json'
}
$('#My_login_form').submit(function(e) {
e.preventDefault();
var pass=$("#My_login_form_password").val();
if (pass.length>0){
loginoptions.data={
password: ($.sha1($("#My_login_form_csrf").val()+$.sha1(pass)))
}
$("#My_login_form").ajaxSubmit(loginoptions);
delete loginoptions.data;
});
The problem with this code is that it is sending a "password" POST variable with the form field value and, a duplicated one with the value I set on "loginoptions.data".
Building off of Cristiano's answer, I was able to get this to work. If you use :beforeSubmit(), the changed value doesn't post, but if you use the :beforeSerialize(), it posts the changed value.
$('#ff').ajaxForm({
beforeSerialize:function(jqForm, options){
var serializedForm = decodeURIComponent(jqForm.serialize());
options.data = serializedForm.deserializeToObject();
options.data.tPassword = MD5($("#tPassword").val())
},
success:function(data){
// do stuff
}
});
If you want to do it anyhow then I think you can use callback function beforeSubmit: function(contentArray, $form, options){}
beforeSubmit: function(contentArray, $form, options){
for(var i=0; i<contentArray.length; i++){
if(contentArray[i].name == "password") {
contentArray[i].value = ($.sha1($("#My_login_form_csrf").val()+$.sha1(pass)))
}
}
}
It seems that ajaxSubmit uses the serialize() function of jquery on the form and then, adds the extra data serialized too. So, if I have a field named "password" with the value "1234" and then try to change that to "abcd", using "loginoptions.data.password", it will serialize everything and put the "options.data" like this:
"password=1234&field_2=value_2&password=abcd"
After many tries, I gave up on using ajaxSubmit function and decided to use ajax function to submit the form:
var the_form=$('form#My_login_form');
loginoptions.url=the_form.attr("action");
loginoptions.type=the_form.attr("method");
var serializedForm=decodeURIComponent(the_form.serialize());
loginoptions.data=serializedForm.deserializeToObject();
var pass=$("#My_login_form_password").val();
if (pass.length>0){
loginoptions.data.password= ($.sha1($("#My_login_form_csrf").val()+$.sha1(pass)));
}
$.ajax(loginoptions);
Here is the deserializeToObject() function:
function deserializeToObject (){
var result = {};
this.replace(
new RegExp("([^?=&]+)(=([^&]*))?", "g"),
function($0, $1, $2, $3) { result[$1] = $3; }
)
return result;
}
String.prototype.deserializeToObject = deserializeToObject;