/    Sign up×
Community /Pin to ProfileBookmark

form posting to itself wipes out dom modification when redrawing form

Hello,

I have a webpage with a form inside it. Below the form is a div. When you submit the form it executes some javascript that adds a bunch of html to the div. I can step through the code in the Chrome debugger and see my dom changes are there, but disappear as I get to the original tags in the page. Is there anyway to prevent this?

[CODE]
<html>
<head>
<title>Search Multiple Craigslists</title>
<script type=”text/javascript”>
function buildIframes() {
alert(“hello”);
var myParms = document.getElementById(“mySearch”).value.replace(” “,”+”) //window.location.search.substring(1);
var myBody = myBody = document.getElementsByTagName(“body”)[0];
var myDiv = document.getElementById(“CraigsListDiv”)
varMyIframes = document.getElementsByTagName(“iframe”)
for (var i = 0; i < varMyIframes.length; i++) {
//Cleanup all the old iframes
myDiv.removeChild(varMyIframes[i]);
}
var myCraigsLists =
[“http://raleigh.craigslist.org”,
“http://blacksburg.craigslist.org”,
“http://boone.craigslist.org”,
“http://charlotte.craigslist.org”,
“http://charlottesville.craigslist.org”,
“http://danville.craigslist.org”,
“http://eastnc.craigslist.org”,
“http://fayetteville.craigslist.org”,
“http://florencesc.craigslist.org”,
“http://greensboro.craigslist.org”,
“http://hickory.craigslist.org”,
“http://onslow.craigslist.org”,
“http://lynchburg.craigslist.org”,
“http://myrtlebeach.craigslist.org”,
“http://norfolk.craigslist.org”,
“http://outerbanks.craigslist.org”,
“http://richmond.craigslist.org”,
“http://roanoke.craigslist.org”,
“http://wilmington.craigslist.org”,
“http://winstonsalem.craigslist.org”];
for (var i = 0; i < myCraigsLists.length; i++) {
var myNewIFrame = document.createElement(“iframe”);
myNewIFrame.setAttribute(“id”,”cl” + i);
myNewIFrame.setAttribute(“width”, “1024px”);
myNewIFrame.setAttribute(“height”, “768px”);
if (myParms === undefined) {
myNewIFrame.setAttribute(“src”, myCraigsLists[i]);
}
else {
myNewIFrame.setAttribute(“src”, myCraigsLists[i] + “/search/” + “pts?query=” + myParms + “&srchType=A&minAsk=&maxAsk=”);
}
myDiv.appendChild(myNewIFrame);
}
}
</script>
</head>
<body>
<form onsubmit=”javascript:buildIframes()”>
<h1>”Enter a search string”</h1>
<input type=”text” id=”mySearch” />
<input type=”submit” name=”mysubmit” value=”Search!” />
</form>
<div id=”CraigsListDiv” />
</body>
</html>
[/CODE]

-Eric

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@cluefulJan 16.2011 — Hello,

I have a webpage with a form inside it. Below the form is a div. When you submit the form it executes some javascript that adds a bunch of html to the div. I can step through the code in the Chrome debugger and see my dom changes are there, but disappear as I get to the original tags in the page. Is there anyway to prevent this?[/quote]
When you submit a form, the page reloads. If you're not sending data to the server, don't submit the form.
Copy linkTweet thisAlerts:
@ewgoforthauthorJan 16.2011 — I changed my form to:

[CODE]
<form>
<h1>"Enter a search string"</h1>
<input type="text" id="mySearch" />
<input type="submit" name="mysubmit" value="Search!" onclick="javascript:buildIframes()" />
</form>
[/CODE]


but it's still doing the same thing. How can I avoid posting the form?

-Eric
Copy linkTweet thisAlerts:
@ewgoforthauthorJan 16.2011 — Hmmm, it looks like getting rid of the form tags and just putting my inputs directly in the body solved my problem.

-Eric
Copy linkTweet thisAlerts:
@SockwaveJan 17.2011 — The problem, as clueful explained, is that you were submitting the form. You can keep the form tag if you want, just change the button to type="button" instead of type="submit". You really don't need a submit button unless you want to send data somewhere.
×

Success!

Help @ewgoforth spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...