0

Hi, say I have <input type="checkbox" id="box1" /> and <div id="createhere"></div> and in a javascript file I have:

function(){
    var box=document.getElementById("box").checked;
    var s = "";
    if(box){
        s = "<input type="text" name="text" id="text" />"
        document.getElementById("createhere").innerHTML = s;
    }else{
        s = "";
        document.getElementById("createhere").innerHTML = s;
    }
}

Now this works BUT it only creates the text box when I refresh the browser(firefox). How can I do the same without refreshing the browser?

2
Contributors
4
Replies
14
Views
2 Years
Discussion Span
Last Post by HuePig
0

You are missing an event to trigger the function. For example ...

 <input type="checkbox" id="box1" onclick="Function Name(argument)">

You'll need to determine the checked state of your checkob. For example after the checkbox is checked, what if the user I checks it.

0

is onclick=function() on javascript library? The challenge is that I cannot use any javascript in my HTML file, all javascript related code MUST be contained in a javascript file.

1

Yes, you can do this from your javascript code without modifying your HTML markup.

for example..

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<input type="checkbox" id="box1" />
<div id="createhere"></div>

<script>

document.getElementById('box1').onclick = function() {
    if (document.getElementById('box1').checked) {
        s = "<input type='text' name='text' id='text' />";
        document.getElementById("createhere").innerHTML = s;
    } else {
        document.getElementById("createhere").innerHTML = "";
    }
}

</script>

</body>
</html>

Edited by JorgeM

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.