jQuery not selector with class match - jquery-selectors

How can I use jQuery to select all div elements that do not contain a class starting with the string 'upper' and do not contain a class starting with the string 'lower'. I tried the following couple of examples with no success:
$('div:not([class^=upper])').filter(':not([class^=lower])')
and
$('div').not('[class^=upper])').not('[class^=lower])')
Please advise...

This is a bit verbose, but it should do the trick -
$('div').filter(function() {
return $(this).attr("class").substring(0,5).toLowerCase() != 'upper' && $(this).attr("class").substring(0,5).toLowerCase() != 'lower'
})

You have to quote the string values in the comparison. In either of your examples above, replace lower with "lower". Do the same for upper.

Try this :not selector
$('div:not([class^=upper,class^=lower])');

Related

Get elements of list beginning with specific letter

I have a list of words allWords and I am trying to create a new list from allWords containing only those words that begin with a specific letter currLetter. Looking at the docs, collection.map() seems like a great choice. However the statement below won't compile since .starts(with: ) returns a boolean.
targetWords = allWords.map { $0.starts(with: currLetter) }
Can anyone point me in the right direction?
While not described in the documentation of collection type instance methods, the solution is to use filter() instead of map():
targetWords = allWords.filter { $0.starts(with: currLetter) }

how to handle empty array for text widget in build method?

How would I handle the objectA[0].name (a string) in the build method if the array is empty?
Text(objectB.objectC.objectA[0].name),
Assuming the array is objectC you can do something like:
Text(objectC.isEmpty? "" :objectC[0].name)
You can read more about ternary operators in dart here
There isn't a great way to do this inline but with a simple extension method to return the original null or the iterable depending on whether the item is null or empty you can make it work.
First
Extension Method
(requires dart v2.7 - update in your pubspec.yaml file)
extension IterableExtension<T> on Iterable<T> {
Iterable<T> get nullWhenEmpty =>
this == null || this.isEmpty ? null : this;
}
Second
To handle null values while you're traversing an object you can use the Dart's conditional member access operator (?.). This operator will only continue with the right-hand side if the left-hand side of the operator is not null. Use the elementAt method on a an iterable to be able to use the ?. operator in the chain. Then, use the ?. operations with the if null operator (??) to get your default value.
Solution
final String value = objectB?.objectC?.objectA?.nullWhenEmpty?.elementAt(0)?.name;
Text(value ?? 'Default Text');
You can, of course, inline the above code instead of using an additional variable.
Resources
Dart Language Tour: Other Operators
Dart Language Tour: Classes
Maybe checking if the list has an element, using isNotEmpty
child: (objectB.objectC.objectA.isNotEmpty)
? Text(objectB.objectC.objectA[0].name)
: Container(),

Multi if statement in class parameters setting

I know that in the latest version of dart we can use if else statements inside the build method. Does anyone know if we can use also if else statement when we setting class parameters? I know I can do inline statement there but inline is a bit hard to read when there are multiple conditions
const int i = 0;
class Person {
// NewClass n = NewClass(a: i == 0 ? 'a' : 'b'); //<- inline statement working
NewClass n = NewClass(a: if(i == 0) 'a' else 'b'); //<- if statement doesn't
}
class NewClass {
final String a;
const NewClass({this.a});
}
Edit:
Basically in my case I've got an TextField widget where I set its's type parameter from enum (Type.text, Type.numeric...) According to this parameter I want to set The textField parameters (textCapitalization, maxLength and so on)
As per your comment, you are already creating an enum for specifying the type of the fields.
enum Type {text, numeric}
Now for specifying the properties of that particular type, you can add an extension on this enum, as shown below:
extension TextFieldProperties on Type {
int get maxLength {
if (this == Type.text) {
return 10;
}
return 12;
}
}
So in your field class you already have a type defined, you can use that type variable to get the properties of that particular type of field.
Type type = Type.text;
print(type.maxLength); // Will print 10
type = Type.numeric;
print(type.maxLength); // Will print 12
Note: It will work only in Dart 2.7 and above
You want the conditional expression (?:), not the conditional statement or literal entry (if), as you have already discovered.
The reason if doesn't work is that if only works as a statement or as a collection literal entry. It doesn't work in arbitrary expressions.
The reason for the distinction is that the if syntax allows you to omit the else branch. That only makes sense in places where "nothing" is a valid alternative. For a statement, "doing nothing" is fine. For a collection, "adding nothing" is also fine.
In an expression context, you must evaluate to a value or throw. There is no reasonable default that we can use instead of "nothing", so an if is not allowed instead of an expression.
Doesn't work because this syntax doesn't exist in Dart. The only way to do what you would like to do is to use the ternary operator.
If you try it in the DartPad you will get an error.
I suggest you to use a function to return the right value.

Function argument with jQuery selector

Folks, this works correctly for me to hide any element where the class name starts with "o"
function hider() {$("*[class^=o]").hide();}
Now I'd like to be able to pass that "o" string in as the function's argument, and I have trouble with the syntax. Any help is appreciated.
It seems that something like this will work:
function hider(startsWith) {
$("*[class^="+startsWith+"]").hide();
}
try it in this fiddle:
http://jsfiddle.net/JECUL/
function hider(className) {$("*[class^="+className+"]").hide();}
Call like so
hider("o");

How to check attr class for string?

How to check attr class for string?
E.G.
HTML:
<p class="p_header"></p>
How to check that 'p' has in it's class '_header'string?
Any suggestions much appreciated.
$("p[class$='_header']")
OR
$("p[class*='_header']")
Use ......className.indexOf('_header') != -1
Do you need more details than that?
If you wanna check for a string within a p's class, you may use a simple search.
Say you have your 'p' inside a div.
$('#your_div p').first().attr('class').search("_header")
This returns -1 if false or no match found, &
index if true or match found.
OR you can directly access your 'p' using other answers submitted here.