473,387 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Can't simulate a "button" in JavaScript

I'm trying to simulate buttons using images. I tried many ways and I don't seem to get it to work as far as the button label is concerned. I tried manipulating "top" and "left" and now the margins and I'm getting bizarre results. This is not the actual code. I did what I had to do to make it independent from the website I'm working on and to "reduce" the problem to the bare minimum factors.

Expand|Select|Wrap|Line Numbers
  1. <style type="text/css">
  2. div.btn {
  3.   width: 80px;
  4.   height: 22px;
  5. }
  6.  
  7. img.btn {
  8.   position: absolute;
  9.   left: 10px;
  10.   top: 10px;
  11. }
  12.  
  13. #btnLabel1 {
  14.   color: white;
  15.   margin-left: 1px;
  16.   margin-top: 1px;  
  17. }
  18. </style>
  19.  
  20. <script type="text/javascript">
  21. function BtnMouseUp(objNum) {
  22.    var lbl = document.getElementById("btnLabel" + objNum);
  23.    alert(parseInt(lbl.style.marginLeft);
  24.    lbl.style.marginLeft = (parseInt(lbl.style.marginLeft) - 5) + "px"; 
  25.    lbl.style.marginTop = (parseInt(lbl.style.marginTop) - 5) + "px";
  26.    document.getElementById("btnImage" + objNum).src = "btn_reg.bmp";
  27. }
  28.  
  29. function BtnMouseDown(objNum) {
  30.    var lbl = document.getElementById("btnLabel" + objNum);
  31.    lbl.style.marginLeft = (parseInt(lbl.style.marginLeft) + 5) + "px"; 
  32.    lbl.style.marginTop = (parseInt(lbl.style.marginTop) + 5) + "px";
  33.    document.getElementById("btnImage" + objNum).src = "btn_prsd.bmp";
  34. }
  35. </script>
  36.  
  37. <body>
  38. <div class="btn" onload="BtnMouseUp('1');" onmousedown="BtnMouseDown('1');" onmouseup="BtnMouseUp('1');">
  39. <img class="btn" id="btnImage1" src="btn_reg.bmp"  />
  40. <span id="btnLabel1">Hello</span>
  41. </div>
  42. </body>
  43.  
Aug 5 '09 #1
1 1580
You can try padding-left of the div instead of the margin of its content. But is it really worth it? All that javascript...
If you just need a button with an image use <button><img/>hello</button>
And jQuery can ease your pain. :)
Aug 5 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: lsarg | last post by:
i've been trying forever to figure out a way to use a regular text link in place of a submit button at the bottom of this. can't get it. i'm just starting to learn php, so i'm stuck. any help at...
5
by: Nick Li | last post by:
Hi, I need to create a html page so that "Back" button(or some other button) is disabled in IE browser. I would appreciate your help. Nick Li
1
by: charliewest | last post by:
I am trying use server controls or web controls to simulate the following behavior. I've an image wrapped w/in a link to create a rollover effect as if both elements we're one graphic: <a...
3
by: madforplaid | last post by:
Can anyone help me to figure out a strange span/radio button/JavaScript behavior? When clicking on the radio button for WEEK 2 below all works okay, but when clicking on the label for the same...
11
by: laredotornado | last post by:
Hello, Is it possible, instead of having the button "Browse" for the HTML <input type="file"> element, to have a graphic in its place? How would this be done? Thanks, - Dave
7
by: xzzy | last post by:
I have several pages in a frame and need the code behind of a page in one of the frames to cause a button in that page to click so the javascript in that button will execute on the client...
3
by: groovymovin | last post by:
Hi there, I am a newbee. searched intensively for a quite simple problem but couldnt find an answer. Here is my problem: I have two layers with 2 movie clips. The movie clip on layer 1 stays...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.