1

I need to be able to change the onclick event of an id so that once it has been clicked once it executes a function which changes the onclick event

Here is my code: Javascript:

function showSearchBar()
    {
        document.getElementById('search_form').style.display="inline";
        document.getElementById('searchForm_arrow').onclick='hideSearchBar()';
    }

    function hideSearchBar()
    {
        document.getElementById('search_form').style.display="none";
        document.getElementById('searchForm_arrow').onclick='showSearchBar()';
    }

and here is the HTML:

<!-- Search bar -->
<div class='search_bar'>
    <img id='searchForm_arrow' src="images/icon_arrow_right.png" alt=">" title="Expand" width="10px" height="10px" onclick='showSearchBar()' />
    <form id='search_form' method='POST' action='search.php'>
        <input type="text" name='search_query' placeholder="Search" required>
        <input type='image' src='images/icon_search.png' style='width:20px; height:20px;' alt='S' >
    </form>
</div>

Thanks

2
  • code does not change the onclick event, i wanted to know if i was calling/defining it wrong Commented Dec 12, 2013 at 16:09
  • @Bull Do you want to Hide and show form tag? Commented Dec 12, 2013 at 16:09

7 Answers 7

2

Change your code in two places to reference the new functions directly, like:

document.getElementById('searchForm_arrow').onclick=hideSearchBar;
Sign up to request clarification or add additional context in comments.

Comments

2

Can you try this,

      function showSearchBar()
        {
            if(document.getElementById('search_form').style.display=='none'){
                 document.getElementById('search_form').style.display="inline";
            }else{
               document.getElementById('search_form').style.display="none";
            }

        }

1 Comment

thanks, that ws going to be my next attempt to try and fiddle with the js code :P
0

You were nearly right. You are settingthe onclick to a string rather than a function. Try:

in showSearchBar()

 document.getElementById('searchForm_arrow').onclick=hideSearchBar;

in hideSearchBar()

 document.getElementById('searchForm_arrow').onclick=showSearchBar;

Comments

0

You do not need to create two function. Just create one function and using if condition you can show and hide the form tag..

function showSearchBar()
        {
            if(document.getElementById('search_form').style.display=='none'){
                 document.getElementById('search_form').style.display=''; // no need to set inline
            }else{
               document.getElementById('search_form').style.display='none';
            }

        }

Comments

0
function searchBar(){
    var x = document.getElementById('search_form').style.display;
    x = (x == 'inline') ? 'none' : 'inline';
}

You can wrap up both functions into one by adding a check to the current condition of the element and applying your style based on that condition. Doesn't actually change the function but doesn't need to as there is now only one functon performing both actions.

Comments

0

With javascript you can check and perform opration

function SearchBarevent()
        {
            if(document.getElementById('search_form').style.display=='none'){
                 document.getElementById('search_form').style.display="inline";
            }else{
               document.getElementById('search_form').style.display="none";
            }

        }

or if you may go for jquery there is better solution toogle

Like:

$("#button_id").click(function(){
$( "#search_form" ).toggle( showOrHide );
});

Fiddle is example

Comments

0

Here is an option that uses jQuery:

$('#searchForm_arrow').click(function() {
    $('#search_form').slideToggle();
});

http://jsfiddle.net/PuTq9/

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.