1

I am trying to convert an <input type="submit"> button to a <button> element along with all it's id, class, events, tabindex etc.

Any idea on a good approach? jQuery is also an option.

Note: I do not have access to the original <input type="submit"> html code.

6
  • 1
    possible duplicate of Change node type Commented Sep 29, 2013 at 14:12
  • @hitautodestruct If you do not have access to the original html you need to do everything manually or your explanation is a little bit unclear Commented Sep 29, 2013 at 14:13
  • 1
    Why? For preventing form submission? If yes, there are better ways for doing this. Commented Sep 29, 2013 at 14:13
  • @Bernhard The original html is generated using an external source. But I can manipulate the source using javascript. Commented Sep 29, 2013 at 15:06
  • @undefined I need to have a button element since I am using an icon font for the text and the value attribute can't take any entities. Commented Sep 29, 2013 at 15:09

2 Answers 2

2
var input = document.yourWayOfSelectingIt('maybeAnID');

input.outerHTML = input.outerHTML.replace(/^\<input/, '<button') + input.value + '</button>';

DEMO

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

5 Comments

what about the rest of the attrbutes? id, tabindex, class, events?
It replaces <input by <submit using regexp, then adds value to the inside and a closing tag. It doesn't do anything with attributes, so they are all conserved. As for events, you should attach them after the replacement, if that can't be done then there is no solution for your problem that I know of - you would have to do that by manually attaching them back one by one.
In jQuery, you can get events using .data('events') but I hear there is a problem with scope. You could also .clone(true) to create new element, but that would put you back in square 1 as you would still need to change the tagName.
Thanks for the simplicity and library independence. works for me.
... but does not work in ie11 apparently. Any idea why?
0

It's a working solution to fetch all attributes and replace the original element with the button element. For the attributes not required on new element a condition can be placed after "this.specified" condition.

    <form action="test.php" action="post">
        <input id="submit" type="submit"  />
    </form>


    <script>
        var oSubmitButton = $('input[type="submit"]');
        var oForm = oSubmitButton.closest('form');
        var sOriginAttrs = '';

        $.each(oSubmitButton[0].attributes, function() {
            if (this.specified) {
                sOriginAttrs += ' ' + this.name + '="' + this.value + '"';
            }
        });

        oSubmitButton.replaceWith($('<button ' + sOriginAttrs + '>').text('Submit').click(function(event) {
            event.preventDefault();

            //Maybe a form submit
            //oForm.submit();
        }));
    </script>

Created a Fiddle

Comments

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.