The task is to hide the <span> tag when href attribute of <a> tag is not present or invalid.
Let us consider we have <a> tag inside the <span> tag. It will look similar to this:
Now we add the id to the elements to identify them.
<span id="outer"><a id="inner" href="www.google.com"></a></span>
Now, we have to hide the <span> tag when “href” attribute of <a> tag is empty or invalid.
- In the second case, the href is pointing to a file/location. We need to check if it is a valid file/location or not. Now in this case when href is present then we check if it points to some valid file or location. We will do it using jQuery Ajax call. The jQuery Ajax call can be used to exchange data with a web server behind the scenes. If it returns an error then the location of the file is invalid.
Example 1: The below code shows when the href is empty.
Example 2: Below code is an example when href points to a location.