jQuery > Update inline script on form submission
- by Andrew Kirk
I am using the ChemDoodle Web Components to display molecules on a web page. Basically, I can just insert the following script in my page and it will create an HTML5 canvas element to display the molecule.
<script>
var transform1 = new TransformCanvas('transform1', 200, 200, true);
transform1.specs.bonds_useJMOLColors = true;
transform1.specs.bonds_width_2D = 3;
transform1.specs.atoms_useJMOLColors = true;
transform1.specs.atoms_circles_2D = true;
transform1.specs.backgroundColor = 'black';
transform1.specs.bonds_clearOverlaps_2D = true;
transform1.loadMolecule(readPDB(molecule));
</script>
In this example, "molecule" is a variable that I have defined in an external script by using the jQuery.ajax() function to load a PDB file. This is all fine and good.
Now, I would like to include a form on the page that will allow a user to paste in a PDB molecule definition. Upon submitting the form, I want to update the "molecule" variable with the form data so that the ChemDoodle Web Components script will work its magic and display molecule defined by the PDB definition pasted into the form.
I am using the following jQuery code to process the the form submission.
$(".button").click(function() {
// validate and process form here
//hide previous errors
$('.error').hide();
//validate pdb textarea field
var pdb = $("textarea#pdb").val();
if (pdb == "") {
$("label#pdb_error").show();
$("textarea#pdb").focus();
return false;
}
molecule = pdb;
});
This code is setting the "molecule" variable upon the form submission but it is not being passed back to the inline script as I had hoped. I have tried a number of variations on this but can't seem to get it right. Any clues as to where I might be going wrong would be much appreciated.