1

I've found quite a bit on how to solve this problem, but I can't seem to get it working. I want to use an image that has the functionality of a button. The following code works well, but is neither great style, nor will it be able to switch the image it uses.

<input type="image" id="mToggle" src="images/chevronright.jpg" width="48" height="48"></input>

I should be able to do something like the following:

HTML

<input type="image" id="mToggle" class="right" width="48" height="48"></input>

CSS

.left{
    background-image: url("images/chevronLeft.jpg");
}

.right{
    background-image: url("images/chevronRight.jpg");
}

JavaScript

$("#mToggle").click(function(){
    $('#mToggle').toggleClass('left');
});

This should give me a button that is initially a right arrow and will swap between left and right every time upon the click.

Edit: I should mention that the second method that I described only results in the default "Submit Query" text appearing for the No picture is displayed.

Thanks in advance.

3
  • input id is missing a ", also I'd also toggleClass('right') Commented Jul 20, 2015 at 18:56
  • That was a typo on my part. It was correct in the code. I've tried both with and without quotes inside of toggleClass Commented Jul 20, 2015 at 18:59
  • maybe remove the src.. and set id="mToggle" your html is not right.. Commented Jul 20, 2015 at 19:01

1 Answer 1

2

See Working fiddle of your code :

HTML :

<input type="image" id="mToggle" class="right" width="64" height="64" value=' '></input>

JS :

Just add the ready funciton $(function(){ }); to specify a function to execute when the DOM is fully loaded.

$(function(){
    $("#mToggle").click(function(){
        $('#mToggle').toggleClass('left right');
    });
});

I hope this will help.

Sign up to request clarification or add additional context in comments.

3 Comments

The text "Submit Query" still appears on my live version. Any ideas as to how to get rid of it?
appear insead the button ? try to add value=" " just like i added in my answer.
That was it. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.