Guide to creating a clickable inline svg in HTML/CSS for use with onclick event in JavaScript

I have a unique inline svg code snippet in my html:

<svg id="nav-search-icon" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32.2 32.2"><path d="M19 0C11.8 0 6 5.8 6 13c0 3.1 1.1 5.9 2.9 8.2l-8.6 8.6c-0.5 0.5-0.5 1.4 0 2 0.5 0.5 1.4 0.5 2 0l8.6-8.6C13.1 24.9 15.9 26 19 26c7.2 0 13-5.8 13-13S26.2 0 19 0zM19 24C12.9 24 8 19.1 8 13S12.9 2 19 2 30...

In order to trigger the visibility of another element by clicking on the svg, I attempted to utilize this javascript code:

let searchIcon = document.querySelector('#nav-search-icon')
let searchBar = document.querySelector('#search-bar')

searchIcon.onclick = function() {
    if (searchBar.style.visibility === 'hidden') {
        searchBar.style.visibility = 'visible'
    } else if (searchBar.style.visibility === 'visible') {
        searchBar.style.visibility = 'hidden'
    }
}

Despite these efforts, the javascript implementation did not yield the expected result. It seems that the issue stems from the fact that the svg element is not interactive when clicked (cursor does not change to pointer/hand). Various attempts were made such as adding pointer-events="all" within the svg and path elements, as well as applying pointer-events: all; in CSS, but none proved successful.

**EDIT: ** Alternative solutions involving wrapping the svg in an <a> tag were explored, although the goal remains to invoke the onclick event directly on the svg element without linking to another page.

Answer №1

I decided to make some adjustments to the JavaScript code, and one of the changes involved enclosing the SVG within an anchor tag (<a>) and attaching the onclick() function to it. Here's how I modified the code:

searchClick = function() {
  let searchIcon = document.querySelector('#nav-search-icon')
  let searchBar = document.querySelector('#search-bar')
  if (searchBar.style.display === 'none') {
    searchBar.style.display = 'block'
  } else {
    searchBar.style.display = 'none'
  }
}
<a href="#" onclick="searchClick();"><svg id="nav-search-icon" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32.2 32.2"><path d="M19 0C11.8 0 6 5.8 6 13c0 3.1 1.1 5.9 2.9 8.2l-8.6 8.6c-0.5 0.5-0.5 1.4 0 2 0.5 0.5 1.4 0.5 2 0l8.6-8.6C13.1 24.9 15.9 26 19 26c7.2 0 13-5.8 13-13S26.2 0 19 0zM19 24C12.9 24 8 19.1 8 13S12.9 2 19 2 30 6.9 30 13 25.1 24 19 24z"/></svg></a>

<div style="display: none;" id="search-bar">
  <input width="200" value="Search"/>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Efficiently shrink column width in Material-UI's <TableRow/> using ReactJS and Material-UI

At the moment, I am utilizing ReactJS along with Material-UI. One issue I am encountering is that when using Material-UI's <Table>, the columns' width are automatically set based on content which results in all columns having equal width. H ...

Using jQuery to retrieve a dynamic element by its ID and store it in an array

I am currently in the process of developing a classifieds website. The search page will feature multiple product cards, each of which can have multiple images. In order to showcase these images, I am implementing a slider using a jQuery library called bxsl ...

What is the best approach for writing a concise Select statement that produces a data list?

Currently, I am working on a small web application using Express.js and SQLite for local use. However, I am facing an issue when trying to perform a full select query on a table. All my scripts are written in JScript in 'use-strict' mode. I am a ...

The Navbar-Burger in Bulma is not linking to menu items properly in Vue.js 2

I have been working on implementing a navbar for my application using Vue 2.0 and Bulma. Everything seems to be working fine on desktop screens, but on smaller screens, the burger icon appears without any elements displayed. It's just there. <temp ...

Tips for recalling the display and concealment of a div element using cookies

My HTML code looks like this: <div id='mainleft-content'>content is visible</div> <div id="expand-hidden">Button Expand +</div> To show/hide the divs, I am using JQuery as shown below: $(document).ready(function () { ...

Submit a set of form variables to PHP using AJAX

I am attempting to transmit a set of form parameters to a PHP script for processing. In the past, I have achieved this using $.post, but now my goal is to accomplish it exclusively with $.ajax. Below is the jQuery click event designed to send all variabl ...

Guide on displaying a document in react-doc-viewer from a protected API endpoint in either Next.Js or ReactJs

I am looking to display files in my Next.JS web application using a secure API. The API provides the following data: { "name": "Test1.docx", "contentUri": "https://api.mypurecloud.ie/api/v2/downloads/x ...

Accessing a text document from a specific folder

As a novice in the world of HTML and PHP, I am attempting to develop a script that can access a directory, display all text files within it, allow for editing each file, and save any changes made (all operations will be managed through an HTML form). Howev ...

Sending files asynchronously using Angular and ng-file-upload through a RESTful API

I have a form with multiple file upload fields. Here is the process: User fills out the form User selects the files to upload User clicks submit Now, here is what I would like to achieve: Submit the form to the server and receive an ID in return Uploa ...

What could be causing the "Circular structure to JSON" error in Node.js when executing a MySQL query?

Currently, I am working on executing a MySQL query to retrieve all the data from a table named LOGIN. If you navigate to https://localhost:2000/select, and provide the parameter in the req.body as table_name with the value set as login. When making t ...

What is the best way to display the ID value as a variable in a bootstrap modal dialog?

I originally thought figuring this out would be a breeze, but it's proving to be quite the challenge. After scouring for solutions without success, I find myself stuck. There's a list of dynamically generated buttons with unique id values that I ...

Always maintaining a responsive and square aspect ratio div

I am developing a CSS and JavaScript-based game that requires the game field to be square and adjust its width and height based on the height of the viewport. In case the viewport width is smaller than the height, I need the field size to adapt while maint ...

Utilizing a drop-down selection menu and a designated container to store chosen preferences

My form includes a select dropdown that displays available options (populated from a PHP database). Users can choose options from the list, which are then added to a box below to show all selected items. However, I am facing a challenge with the multiple s ...

What is the best way to ensure that a css grid using grid-template-columns: repeat(auto-fit, minmax(x,y)) distributes items as evenly as possible across each row?

I'm in the process of creating a CSS grid. Here is a snippet of the CSS I am using: div.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); } Within my HTML code, there are exactly 6 items: <div class=st ...

Users using an iPhone are unable to adjust the scale or scroll on this website

I find myself wondering what I might be overlooking in this situation Here is the HTML code: <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="shortcut icon" href="../../nImg/favicon.ico" ...

What could be causing this error I'm receiving: instance versus prototype difference?

Can someone help me understand the difference between .prototype and regular instances in JavaScript? I am confused about why this code is not working and getting a type error: "undefined is not a function". I am experimenting with the Ninja() class and ...

Restrict magnification in THREE.js mousewheel interaction

Seeking advice on limiting zoom in and out of a model in three.js. I have explored trackball controls, but couldn't find a way to restrict zooming. I also experimented with orbit controls, but encountered issues when combined with trackball controls r ...

Submitting form data in Angular is a straightforward process

I'm currently using the ng-flow library to upload images to my server. After a user drags and drops an image, I can successfully retrieve the HTML 5 file image in my controller. However, when trying to download it to the server along with other parame ...

Is there a jQuery function that can produce repeated output and append content with each successive click?

Attempting to implement a dynamic searchbar with various parameters has led me to explore using jQuery to load and clone the searchbar file in order to append it to the body dynamically. I have made several attempts to modify selectors without achieving t ...

Learn the process of updating a specific section of a Facebook share link using AJAX dynamically

Here's an interesting scenario I'm dealing with. I'm trying to create a modification to the Facebook share button link where a portion of the link is replaced by a user ID number. This way, when someone clicks on the shared link, it will dir ...