-1

I'm having trouble getting my code to correctly display HTML within the JavaScript code I have. Basically, I am checking for the variable hundo to equal 3, and then have the script display a form. I have tested the code to just show an alert message when hundo equals 3, but when I use document.write it fails to work.

<script>
setInterval(function(){
if(hundo == '3'){
document.open();
document.write("<form action="insert.php" method="post">
Link URL: <input type="text" name="linkurl" />
<input type="submit" />
</form>");
document.close();
}
}, 1000);
</script>

2 Answers 2

0

You need to alternate the quotes you are using in your document.write:

document.write('<form action="insert.php" method="post">
Link URL: <input type="text" name="linkurl" />
<input type="submit" />
</form>');

The problem is that you are only using one type of quotes, so javascript sees "<form action=" than an undefined javascript object: insert.php, without any operator between them, to boot.

Sign up to request clarification or add additional context in comments.

7 Comments

What you say makes complete sense and yet it is still not working.
"it is still not working" - meaning what exactly? What does the error console say?
The error console says nothing the form just doesn't show up when hundo = 3.
@user1126681 - Where do you expect it to appear? document.write would end up at the top of the document. You should probably use the DOM instead of document.write, constructing the elements in memory and appending to a known node.
@user1126681 - Several ways, but using the DOM is (in my opinion) best. An alternative is to have a div with an id, use document.getElementById using that id and setting the innerHTML property to the new form.
|
0
<script>
setInterval(function(){
if(hundo == '3'){
document.open();
document.write('<form action="insert.php" method="post">
Link URL: <input type="text" name="linkurl" />
<input type="submit" />
</form>');
document.close();
}
}, 1000);
</script>

This will work..:)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.