Jump to content

New to javascript I know this may be simple..


simeonC

Recommended Posts

I would like to prompt a form when a certain element is clicked I have the

onClick attribute and know how to call a function.

 

I need to know how to display a form through javascript that will then be inserted into an sql database...

So if i can be placed at a starting line that would be great

Just do

<a href="#" id="new_form">Click here to launch new form.</a><br><br><span id="formOP"></span>
and in your JavaScript part, do this (easy solution with jQuery is in a spoiler below).

 

function returnForm(){
var form = '<form method="POST" action="" name="newform"><fieldset><dl><dt>Username</dt><dd>Enter your username: <input type="text" name="username" placeholder="Username"></dd></dl></fieldset></form>'; return form;
}
function doForm(){
document.getElementById("formOP").innerHTML = returnForm();
return false;
}
window.onload = function(){
var c = document.getElementById("new_form");
c && c.onclick ? c.onclick = doForm() : return false;
};

// We assume jQuery and our returnForm() functions are still defined
jQuery(document).ready(function(){
jQuery('#new_form').click(function(e){
e.preventDefault();
e.stopPropagation();
jQuery('#formOP').html(returnForm());
});
});

 

 

Edit: Fixed a small quotation error. Also, this is just a dummy function, you have to style and customize this code, of course.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.