Link to home
Start Free TrialLog in
Avatar of WestCoast_BC
WestCoast_BCFlag for Canada

asked on

span onclick not working if no string displayed

If I have:
<span onclick="myfunc()">hello world</span>

Open in new window

myfunc is triggered if I click on the text: hello world

How can I get the function to be triggered if there isn't any text in the span. Ie, if it looks like:
<span onclick="myfunc()"></span>

Open in new window


Thank you for any help you can provide
Avatar of Russ Suter
Russ Suter

A span with no content has no width and therefore nothing to click on. You'd need to put at least something, perhaps a non-breaking space character in there. Alternatively, you could use CSS to set a minimum width but you might also have to change the span's display property to inline-block to get that to work.
Depends on what you are trying to achieve... even a hidden button would non work like that as Russ has already said, you'll need some dimensions otherwise the element would have a height and width of zero.

You could either add width and height to your span or put another element within the span like an image but that's then another bunch of bytes to load. Adding a colour background would give it the appearance of a button or change your SPAN to a block level element like DIV or one of the headings, H1-H6.

Easier still, change it to a BUTTON which wouldn't necessarily need the background colour option.

<button type="button" style="width:60px; height: 20px;" onclick="myfunc()"></button>
Avatar of WestCoast_BC

ASKER

I have a table that includes a column of text. I want the user to be able to click on the text and edit it. Some cells may not contain any initial text but I want the user to be able to select the cell and edit the text. A button does not work for me since I want to show the cell empty if there isn't any text.
ASKER CERTIFIED SOLUTION
Avatar of Justin Pilditch
Justin Pilditch
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you. I was missing the inline-block which was why the min-height an min-width was not working