Using Div for onclick? is that good. What is the right way?

<button type="button" onclick="displayResult()">
        Insert new row</button>

but I want to do is,

<div onclick="displayResult()"> Insert new row</div>

The function do fire ok.  Is div a right way to do onclick on a text? I do not want a look of a button

/* css */
div.link {
cursor: pointer;
text-decoration: underline;
}
goodkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Nothing wrong with using 'onclick' on any element that will accept it including a 'div'.  You may want to add instructions that tell the users what needs to be done if it does not look like something to click on.
0
Julian HansenCommented:
The function do fire ok.  Is div a right way to do onclick on a text? I do not want a look of a button
You can drive your car with your feet if you want to - does not mean it is a good idea.

Making a <div> behave like a button because you don't like the way the button looks - well it will work but it is like the example above. That is what CSS is for - use a <button> <input> or <a> and style it to look the way you want it to - would be the generally accepted practice.

So to answer your question

Using Div for onclick? is that good. What is the right way?

It will work - but no it is not considered the right way to do it in most situations. Styling a button or an anchor would be the most common approach.
0
goodkAuthor Commented:
so can we style the button to look like a <a href ?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Julian HansenCommented:
Yes - here is some sample code (working link here)
<!doctype html>
<head>
<title>Test</title>
<style type="text/css">
.my-styled-button {
  border: 0;
  padding: 0;
  margin: 0;
  background: none;
}
.my-styled-button:hover {
  text-decoration: underline;
  cursor: pointer;
}
</style>
</head>
<body>
<button class="my-styled-button">Click Me</button>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
goodkAuthor Commented:
thank you
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.