views:

23

answers:

1

Hi i want to create a button which has caption (ex Category) and the Image of it in the Button too.

so we can say that button has both Caption + Image

I hav to use javascript only

A: 

This creates button with javascript and put lable "Category" to it.

var btn = document.CreateElement("input");
btn.setAttribute("type", "button");
btn.setAttribute("value", "Category");

var frm = document.forms[0];
frm.appendChild(btn);

You can't have Text + Image to button as far as i know, either you can have text or image. You can specify an image for it like this though:

var btn = document.CreateElement("input");
btn.setAttribute("type", "button");
btn.setAttribute("value", "Category");
btn.style.backgroundImage = "url(path)"; // for image

var frm = document.forms[0];
frm.appendChild(btn);

....

Other Possibility:

If you want to apply Text + Image, then you should create an image containing both of these and specify that as background for the button.

Update:

If you set padding on the button to account for the width of the background image and set the background image to no-repeat you can have both.

Suggested By Andrew in the comments below, thanks

Sarfraz
but it doesnt include image in the same button
Khilen
i want "category"+"image" both
Khilen
@Khilen: you can't have both, you will have to create an image containing both text + image so that will be one image for both. see my answer again.
Sarfraz
@Khilen: Also if you won't accept answers, your acceptance rate will never increase and will always remain at 0%. :)
Sarfraz
if you set padding on the button to account for the width of the background image and set the background image to no-repeat you can have both.
Andrew Neelands
@Andrew Neelands: hey thanks i had never tried that, i update my answer with that.
Sarfraz
Sorry but it doent workingi have wriiten what u have suggested me but its not workingthe btn.style is not defined
Khilen