Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6174519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:46:03+00:00 2026-05-23T23:46:03+00:00

Is there anyway to attach a textInput or keypress (or indeed any other text

  • 0

Is there anyway to attach a textInput or keypress (or indeed any other text input) event to an SVG Element when running SVG inside HTML in a browser?

I can attach an eventhandler to document.documentElement but nothing happens when I attach an eventhandler via whatever mechanism to an SVGElement of anykind … SVG, g, rect…

I see from the SVG spec that this is not supported (W3 SVG list of events), but do any Browsers support any ‘extras’?

I cannot get the following to work on Chrome or Firefox…

<!DOCTYPE HTML>
<html>
<head>
<title>Key event test</title>
<style type="text/css">
body,html {
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
    background: #f00;
}

#main {
    margin: 0;
    padding: 0;
    height: 100%;
    width: 80%;
    background: #0f0;
    float: right;
}

#drawArea {
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
    background: #ffffff;
}

#side {
    margin: 0;
    padding: 0;
    height: 100%;
    width: 20%;
    background: #00f;
    float: left;
}


</style>
<script type="text/javascript">

    function createBoundEventHandler(el, name) {
        var f = 
            function eventHandler(ev) {
    if("mousedown" === ev.type) {
        el.focus();
    }
    else {
        alert("asEventHandler: " + el.localName + " " + name + " " + ev.type);
    }
    return true;
            };
        return f;
    }

    function doEvent(event, elementName, eventName) {
        alert("asAttribute: " + elementName + " " + eventName + " " + event.type);
        return true;
    }

    function addEventHandler(element, eventName, type) {
        var attrName = "on" + eventName;
        var attrValue = "doEvent(evt, '" + element.localName + "', '" + eventName + "')";
        if("asAttribute" === type) {
            element.setAttribute( attrName, attrValue);
        }
        else {
            element.addEventListener(eventName, createBoundEventHandler(element, eventName), false);
        }
    }

    window.onload = function() { 
        var eventHandlerType = "asEventHandler"; //asAttribute asEventHandler

        var svgTarget = document.getElementById('drawArea');
        var svgRect = document.getElementById('aRect');
        var nonSVG = document.getElementById('side');

        addEventHandler(svgTarget, "keypress", eventHandlerType);
        addEventHandler(svgTarget, "mousedown", eventHandlerType);
        addEventHandler(svgTarget, "keyup", eventHandlerType);
        addEventHandler(svgTarget, "keydown", eventHandlerType);

        addEventHandler(svgRect, "keypress", eventHandlerType);
        addEventHandler(svgRect, "mousedown", eventHandlerType);
        addEventHandler(svgRect, "keyup", eventHandlerType);
        addEventHandler(svgRect, "keydown", eventHandlerType);

        addEventHandler(nonSVG, "keypress", eventHandlerType);
        addEventHandler(nonSVG, "mousedown", eventHandlerType);
        addEventHandler(nonSVG, "keyup", eventHandlerType);
        addEventHandler(nonSVG, "keydown", eventHandlerType);

        //We can get a keypress if we attach an event handler to the underlying document Element    
        addEventHandler(document.documentElement, "keypress", eventHandlerType);
    }
</script>
</head>
<body>
    <div id="side" tabindex="0">
    </div>
<div id="main">
    <svg  version="1.1" xmlns="http://www.w3.org/2000/svg" id="drawArea" focusable="true" tabindex="0"> 
    <rect x="0" y="0" width="100" height="100"
            style="fill:yellow; ;stroke-width:2; stroke:rgb(0,0,0)" id="aRect" focusable="true"/>
    </svg>
</div>
</body>
</html>

As the example shows I can still fallback on key events on the ‘background’ doc element. Does anyone have any pet libraries, techniques for relating this back to an SVGElement.

N.B. I have also tried tabindex(0) and focus to no effect…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:46:05+00:00Added an answer on May 23, 2026 at 11:46 pm

    A simple answer might be that the ‘focusable’ attribute isn’t supported in those browsers. For key event listeners on e.g a <rect> to make any sense there needs to be a concept of focus inside the svg.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I attach a click handler to a button on my page. Is there anyway
Is there any way to automatically launch an application on USB attach or CD
Is there anyway to have items in an ASP.NET DropDownList have either their Text
Is there any way to attach a listener to FullCalendar that will be fired
Is there any way to attach Oracle form Builder with ms access to make
Is there any way to bind a mouse event such as 'mouseover' to a
Is there any way in jQuery to attach a listener to a <td> cell
I have a dotnet windows service that's currently hung, but running. Is there anyway
Is there any way to attach custom data to elements? Kind of like the
I'm trying to attach a video clip (.avi) to an email. Is there any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.