two functions for one onsubmit - html

I am trying to get two function to work on one onsubmit, I made this code:
onsubmit="return validateForm1(); return validateForm2()">
but this makes validateForm1() function just work and validateForm2() doesn't ,
how to make both functions work.

You can't return 2 times in a row. First return will complete the logic of your function.
Instead you should use something like this:
var myElement = document.getElementById("myElement");
myElement.onsubmit = function(){
validateForm1();
validateForm2();
}
Also you shouldn't use inline javascript the way you do this right now. I think you might find this article useful.

You can't have to functions returning at this point. You could use the result of both in a combined call:
function validateBoth() {
return validateForm1() && validateForm2();
}

Related

pass a local variable to an click function

I want to pass a local variable from one function to another and I have tried some solutions but they didn't work because I have a click function, I need to put the variable first of all and I don't how to do it, also I declared the variable outside the function but if I use it outside of all the functions it doesn't has all its values or inside the function resaltar nothing appears, any help is welcome
let children=$('div[class^="fila"], div[class^="pieceRow"]' ).children()
var clase
$(children).each(function getClass(){
clase=$(this).attr('class')
$(clase).on('click', function resaltar(){
if (clase==clase) {
$(this).addClass('shadow')
}
})
})
this is the html code https://jsfiddle.net/qb5fwcus/
Please try this code :
let children = $('div[class^="fila"], div[class^="pieceRow"]' ).children();
$(children).on('click', function(){
var clase = $(this).attr('class');
resaltar(clase);
})
function resaltar(clase){
$('.shadow').removeClass('shadow');
$('.' + clase).addClass('shadow');
}
Explanation : You can not pass any value for the callback function for any event handler. Either it can be an anonymous function, or a function, not requiring any argument. However, you can achieve that, by making the callback function anonymous, and call any function from it. In this way, you can pass variables.
PS : Let me know if I got it wrong in any manner :)
Let's assume that you will be passing it to a pure JS function.
function myFunc() {
console.log("My function!");
}
In your 'click', you're calling the function ''resalter'', that you're also defining on the spot.
You want to call myFunc, so:
$(clase).on('click', myFunc())
Now, myFunc is not expecting a variable. Let's just pass a variable:
function myFunc(myVar) {
console.log("Passing a variable of type: " + typeof myVar);
}
Now, you're only expected to pass this var in the function you're calling. Given the previous example I gave, we have:
let x = 1; // our variable
$(clase).on('click', myFunc(x))
This way you're passing 'x' as a variable, of type integer. Use this code as inspiration to try and reach your goal. It is a bit hard to give a more exact answer, given that we don't know what variables have to be passed to what function and what the purpose is.
Good luck!

Have to use manual wait() to get CodeceptJS/Puppeteer custom helper to see table(td tr)

I have a codeceptjs/puppeteer project and am building a custom helper for accessing information in tables. I have been able to make this work, but only by putting a two second wait in my test step before calling on the async function in my custom helper class. Given that this is all based on async/await, I have to believe I am just missing something and there is a clean way to do this.
Function from my helper class.
async getRowCount() {
//const browser = this.helpers['Puppeteer'].browser;
const page = this.helpers['Puppeteer'].page;
page.waitForSelector('tbody');
let rowCount = await page.$$eval('tbody tr', rows => rows.length);
return rowCount;
// These work
// page.waitForSelector('a[href="#/site/create"]');
// page.click('a[href="#/site/create"]');
}
My codeceptjs scenario is below.
Scenario.only('Table check ALL', async (I, loginAs) => {
loginAs('bob');
I.say(await I.getRowCount());
I.wait(3);
});
When the code is as shown above, my row count that is returned in always 0.
However, if I put a 1 second wait just before the I.getRowCount() function, then the correct total number of rows for the tbody tr selector is returned.
If anyone can help me understand why this is happening and what I can do to fix it so I don't have to pepper my code with manual wait steps to accommodate these helper functions (core "feature" of codeceptjs), I would greatly appreciate it.
Thank you!
You need to await waitForSelector:
await page.waitForSelector('tbody');
Almost all page methods are returning promises, so you have to await them.

React Native ArrowFunction Against normal function (Appstate)

I am trying to find out the difference between 2 functions. In my
react native app I use an AppState eventlistener to check if the app
is running in the background or foreground (see code below):
AppState.addEventListener('change', this._handleAppStateChange);
the function looks like this:
_handleAppStateChange = (nextAppState) => {
console.log('nextAppState', nextAppState)
this.setState({
appState: nextAppState
});
};
I did not like the way te function is made because in the listener you call it without a parameter
but it the function itself it has a parameter. So I wanted to change this to make it more clear.
This is what is made:
_handleAppStateChange (nextAppState) {
console.log('nextAppState', nextAppState)
this.setState({
appState: nextAppState
});
};
This works fine the nextAppState is still logged but the setstate does not work anymore
is says that it`s not a function. Can someone explain me why?
And can someone explain me which of both functions is the best to use.
Thx a lot!
Start using arrow functions, one of the reason why arrow functions were created is for the problem you have mentioned i.e losing this in the function context.
So in order for your function to work you need to bind the function to this.
In your constructor add this line
this._handleAppStateChange = this._handleAppStateChange.bind(this)
Or you can replace this function
AppState.addEventListener('change', this._handleAppStateChange);
to
AppState.addEventListener('change', (nextAppState) => this._handleAppStateChange(nextAppState));
Bonus.
If you need to pass extra parameter other than nextAppState or which is in state. This line wont work
AppState.addEventListener('change', (nextAppState, this.state.someRandmValue) => this._handleAppStateChange(nextAppState, this.state.someRandmValue));
As this.state.someRandomValue is undefined since handleAppState wont emit this value instead use this
AppState.addEventListener('change', (nextAppState) => this._handleAppStateChange(nextAppState, this.state.someRandmValue));

Error with custom Search and Replace function for Google Sites

I'm trying to use a script to replace a particular string with a different string. I think the code is right, but I keep getting the error "Object does not allow properties to be added or changed."
Does anyone know what could be going wrong?
function searchAndReplace() {
var teams = SitesApp.getPageByUrl("https://sites.google.com/a/directory/teams");
var list = teams.getChildren();
list.forEach(function(element){
page = element.getChildren();
});
page.forEach(function(element) {
var html = element.getHtmlContent();
html.replace(/foo/, 'bar');
element.setHtmlContent = html;
});
};
Try This:
Javascript reference:
The replace() method returns a new string with some or all matches of a pattern replaced by a replacement.
I think the issue here is that forEach cannot change the array that it is called upon. From developer.mozilla.org "forEach() does not mutate the array on which it is called (although callback, if invoked, may do so)."
Try doing it with a regular loop.

Function call on ngClass not working

I have a problem with a ngClass and a function with building an app with Ionic 2 and Firebase.
The function does work, but the value of the function does not come in the HTML code. I have used this as an example: https://codepen.io/andre13/pen/oecgi.
The html and the call for the function looks like this:
<button ion-button class="chatbutton" [ngClass]="compareUser(user.$key)"
(click)="gotoDoctorChat(user.$key)" >{{user.naam}} </button>
The function looks like this:
compareUser(uid){
this.angFire.database.object('/users/' + uid +'/'+ this.coach1+'red').subscribe(snapshot=>{
console.log("snapshot.tostring = " + snapshot.$value);
if(snapshot.$value === '0'){
return 'unread';
}else{
return null;
}
})
};
The CSS looks like this:
.unread{
background-color: red!important;
}
Please ask if something is not clear.
Try enclosed by single quote (') like this
return "'unread'";
and also make sure you have imported the correct module in the main component
import { NgClass } from '#angular/common';
The reason is because function compareUser always returns undefined before the internal function call is resolved.
You can test this with:
console.log(compareUser(aUID));
Difficult to suggest an alternative without knowing more of your logic/requirements.