Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is it possible to put a JavaScript code within the HTML Form tags. I am trying to achieve something like this:

function abc(abcd) {
    document.getElementById("context").innerHTML += "<br> 1:";
    document.getElementById("context").innerHTML += "***<form METHOD=POST ACTION=\"/InformationRetrieval/home\">***<input type=\"checkbox\" name=\" GoogleRelevance\" value=\"1\"> Relevant <br><br>";

    document.getElementById("context").innerHTML += "<br> 2:";
    document.getElementById("context").innerHTML += "<input type=\"checkbox\" name=\" GoogleRelevance1\" value=\"1\"> Relevant <br><br>";

    document.getElementById("context").innerHTML += "<br> 3:";
    document.getElementById("context").innerHTML += "<input type=\"checkbox\" name=\" GoogleRelevance3\" value=\"1\"> Relevant <br>***<input type=\"submit\" value=\"Submit\"></form>***<br><br>";
}

Here what I am trying to ask in this example is: I have started the tag initially in my starting 2-3 lines, but I am not closing it there and rather inserting some more javascript lines and in the end I am closing the tag with another <input type="submit">, but when I click this button it does not work.

I tried using the OnClick property too but that didn't work either.

I am badly stuck with it. Any help will be greatly appreciated.

share|improve this question
what exactly are you trying to achieve, can you post more code? Are you trying to fill in a input field or dynamically rendering the form? Not clear from the code above... Thanks – Leon Nov 30 '11 at 12:01
please don't repeatedly modify the innerHTML property. Build your string in a local variable and perform the lookup and modification once. – nickf Nov 30 '11 at 12:03
I am trying to dynamically render the forms. The scenario is like I am getting results from Google API. For every result I am displaying a checkbox associated with it. And at the end of the page , that is after getting search results I am adding a Submit Button and trying to go to the particular servlet with all the form input check box informations. – Start0101End Nov 30 '11 at 12:04
which tag you have not closed? can you format your code and post it. Remove document.getElementById("context").innerHTML in every line and instead assign it at the end (using local variable). – Sandeep G B Nov 30 '11 at 12:04
@nickf I was doing the lookup at once only initially but it made me more struck when I had to assign one check box with every result and to display the submit button at the end of all the results , passing values of all the CheckBoxes to the servlet. – Start0101End Nov 30 '11 at 12:06
show 4 more comments

1 Answer

I think you really should use some good ajax library (jQuery, YUI, dojo, goog, ...) for something like this, stuffing all the code into form attributes doesn't feel right. As nickf pointed out modifying a dom property in this way is really not a good idea. As I'm used to jQuery here is a snippet to what I understand from your post is the problem:

// Caching the stuff that you used to += on the innerHTML
var newStuff = ' /* your form code here */ '; 

jQuery
.ajax({
    url: /* your request to google */,
    context: jQuery('#context') // Caching the context dom node
})
.done(function() { 
    // The request was successful
    jQuery(this) // The cached context node is the context of this callback
    .append(newStuff); // You only have to append new stuff here, if you want something
                       // more dynamic insert the logic in this function
})
.fail(function() { /* alert("error"); */ })
.always(function() { /* alert("complete"); */ });

I didn't test this one though, it's only a recommendation. To me it is cleaner and it has the advantage if you get it to work in one browser this way you can count on jQuery to make sure it's working in most other browsers too.

And by the way: you can use single ' around the whole html string so that you don't need to escape the " thingys.

Hope this helps, good luck!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.