javascript - If label contains this and not this prepend * -


i trying add * if label contains city, state, , zip. want happen if * not exist in label already. not sure if can concatenate contains , not contains together. - cannot edit forms , fields have * randomly put in. here 2 ways failed me.

here fiddle-https://jsfiddle.net/4o24kylw/2/

here jquery

//$("label:contains('city'):not(:contains('*'),label:contains('address'),label:contains('state'),label:contains('zip')").prepend( "* " );   $("label:contains('city'):not(:contains('*'),label:contains('address'),label:contains('state'),label:contains('zip')").prepend( "* " ); 

got answer... maybe can take in different direction... can simplify contains. similar label:contains('city', 'state', 'etc...'):not(:contains('*')).prepend( "* " ) or maybe way works :]

this way if cannot simplified - $("label:contains('city'):not(:contains('*')),label:contains('mailing address'):not(:contains('*')),label:contains('state'),label:contains('postal code'):not(:contains('*'))").prepend( "* " );

rather setting complicated query, check text within prepend function itself:

$('label:not(:contains("*"))').prepend(function(_, txt) {   return 'addresscitystatezip'.indexof(txt)>-1 ? '* ' : ''; }); 

fiddle


indexof function looks text within string, , returns -1 if not found.

so if txt 'city', 'addresscitystatezip'.indexof(txt) return 7.

the conditional (ternary) operator returns asterisk if there's match – otherwise, returns nothing.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -