Jump to content

Recommended Posts

You should define the input within/as a hidden element, then display when you capture the click event on the anchor. You should always provide a non-JavaScript version though, so you would either use JavaScript to hide the element as the page loads, or ensure that the anchor's href works to provide non-JavaScript support. I'll give you some example code for the first solution...

 

In the HEAD:

<script type="text/javascript">
window.onload = function() {
    document.getElementById('target_anchor').onclick = function() {
        document.getElementById('input_block').style.display = 'block';
    }
}
</script>

 

This will wait for the page to finish loading, then assign a function to the click event of the #target_anchor element - that function will handle displaying the element. Assigning the events this way is part of what unobtrusive JavaScript is all about, as you split the JavaScript logic from the HTML.

 

Note that you can only have one window.onload declaration, so if you already have one, or add one later, you'll need to merge them.

 

In the BODY:

<a id="target_anchor">Click me</a>

<div id="input_block">
    Some input: <input type="text" />
</div>

<script type="text/javascript">
    document.getElementById('input_block').style.display = 'none';
</script>

 

This second block of JavaScript is placed within the body so that it hides the element as the page is loading, not when it's finished (so you don't see it suddenly disappear at the end). Doing it this way means that when you reload the page with JS disabled, the input will already be visible.

 

Hope this helps.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.