6

I have an animated search form as below.

<form role="search" method="get" id="searchform" action="<?php echo home_url('/'); ?>">

<div id="searchtext">
    <input type="text" value="" size="25" name="s" id="s" placeholder="<?php esc_attr_e('Search', 'reverie'); ?>">
</div>

<div id="submitsearch">
    <input type="button" id="searchsubmit" value="" class="button postfix">
</div>

jQuery("#submitsearch").click(function () {
   jQuery("#searchtext").animate({width:'toggle'},500);
      jQuery(this).toggleClass("closed");
}); 

When the search button is first clicked, it toggles out the text area where a user can type in what they want to search. When they click the search button again however, it should submit the search form (i.e. the input type should change from button to submit).

I've read a few posts on SO already, however these are from a few years ago & I'm not sure if they are still relevant. Is it not possible to change an input's type?

1
  • Why not just have a 'state' property on the button that keeps track of the current step? First time you click it sets the state to 'submit'. Second time you click you do form.submit(). Something about changing the type attribute doesn't sound good to me. Commented Apr 9, 2014 at 15:31

3 Answers 3

9

I think it is possible, because type is an attribute. Follwing small code line should help:

$('#searchsubmit').removeAttr("type").attr("type", "submit");

Maybe there is a way with less risk. The way is to add an 'onclickEvent' that called a submit function:

 $('#searchsubmit').on('click', function () {
    $(this).parents("form:first").submit();
});
Sign up to request clarification or add additional context in comments.

2 Comments

Why do you first remove the type? Can't you simply just replace / change the type property?
@Steven I do that only for safety. You can leave it out if you want
1

You can remove the type attribute and set it again as submit.

jQuery("#submitsearch").click(function () {
jQuery("#searchtext").animate({
    width: 'toggle'
}, 500);
jQuery(this).toggleClass("closed");
var btn = document.getElementById('searchsubmit');
btn.removeAttribute('type');
btn.setAttribute('type', 'submit');
});

Working fiddle.

3 Comments

Why do you mix DOM with jQuery? Why not just use jQuery?
using jquery attr function won't override it .. check this fiddle jsfiddle.net/Karim_AG/mSzY5/1
the type would still be button
1

Something like this should do it:

$('.button').on('click', function(){
  $("#searchtext").animate({width:'toggle'},500);
  $(this).attr('type', 'submit');
});

It's not complete, but should get you closer to what you want :)

hmm... I think that with the latest jQuery, you can even replace on('click' function(){}) with .click( function(){} );
(The old version of .click() did not have the live / on bindings)

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.