Hi there! I have a snippet I often use on a type of web page, and it responds to that page:
{mechtype={site: text; selector=.proof-name__name}}
{if: contains({=mechtype},"Cover")}
Display all this cover stuff
{else}
Display interior stuff
{endif}
But sometimes I have reason to use the same snippet on a PDF that obviously doesn't have that selector field. What would I do to set up something like "if no match found for selector" and then have it go this way instead:
{formmenu: default=cover; interior; name=mechtypemanual}
{if: mechtypemanual="cover"}Display all this cover stuff (etc.)
And -- is the best way to have the same cover info displayed whether it's triggered by the selector text or the dropdown menu to have it in a separate snippet I import?
Thanks!
Hey @megan.gendell ,
Great question! There's two ways you can accomplish this. Both do the same thing, so it's whatever makes the most sense to you.
-
Use the catch() function: this function basically says "if it's an error, use this default value instead"
{mechtype=catch({site: text; selector=.proof-name__name}, "Backup value")}
{=mechtype}
^^ essentially, the catch does nothing if it's not an error, then uses the backup value if it is an error
-
Use the iserror() function: this detects if something is an error and returns yes/no. You can use it in an if statement to do what you want.
{mechtype={site: text; selector=.proof-name__name}}
{if: iserror(mechtype)}Display this text if there's an error{else}Display this text if there's not an error{endif}
2 Likes
Thank you! The iserror() seems like the best option here because it will allow me to nest further dropdown menus, if statements, etc in the iserror area. Is there a way to avoid having the "No match found for selector" error message displayed in the snippet, though? I don't want users to feel they've done something wrong or there's a problem -- it should be seamless if they are using this snippet somewhere other than the website that contains this selector.
Perfect, thank you!
I didn't know about the preview=no setting for notes, I will use this in other ways too. Thanks!
Hey! Adding an alternative to Andrews answer. I have used something like this, so that when there is no match it gives a textfield instead
{note: preview=no}{fn={site: text; selector=[aria-label="First Name"]}}{firstname={=extractregex((fn), "[^\s]+")}}{endnote}{if: contains(firstname, "Error")} {formtext}{else}{=firstname}{endif}
2 Likes