1

Basically I need to change the elements in a form based on a choice (radio button perhaps). So that 2 forms are potentially available on a page.

So far I've come up with this but it doesn't seem to work...

//javascript

function FormChange(toChange){
    if (toChange){
        var oldHTML = document.getElementById('li1').innerHTML;
        var newHTML = "Company Name: " + "<input type="text" name="companyname" />";
        document.getElementById('li1').innerHTML = newHTML;
}

//HTML

<form action="insert.php" method="post">
<li id="li1">Firstname: <input type="text" name="firstname" />
</form>

<input type = "button" value="Change that bad boy" onclick="FormChange(true)"/>

My Intention was to remove the firstname field and replace it with the companyname field.

Any help is greatly appreciated, thanks.

2
  • You don't close the li tag... that could be an issue. Commented Apr 25, 2012 at 17:48
  • 1
    If you combine the two answers thus far you will probably get working code. It is important when you have an issue with javascript to check and see if there are any errors on the console. This definitely would have thrown a few. Commented Apr 25, 2012 at 17:50

6 Answers 6

1

Putting the two current answers together, and adding a little error handling:

function FormChange(toChange) {
    if (toChange) {
        var elt = document.getElementById('li1');
        if (elt) {
            var newHTML = "Company Name: " + "<input type='text' name='companyname' />";
            elt.innerHTML = newHTML;
        }
    }
}


<form action="insert.php" method="post">
<ul>
<li id="li1">Firstname: <input type="text" name="firstname" /></li>
</ul>

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

Comments

1
function FormChange(toChange){
    if (toChange){
        var oldHTML = document.getElementById('li1').innerHTML;
        var newHTML = "Company Name: " + "<input type='text' name='companyname' />";
        document.getElementById('li1').innerHTML = newHTML;
    }
}

DEMO.

Comments

0

try escaping the inner double quotes in your innerHTML text.

Comments

0

You forgot to close the {!

Fix:

function FormChange(toChange) {
    if (toChange) {
        var oldHTML = document.getElementById('li1').innerHTML;
        var newHTML = "Company Name: " + "<input type="text" name="companyname" />";
        document.getElementById('li1').innerHTML = newHTML;
    } // <-- this was missing!
}

1 Comment

Oh sorry, in my code it exists but i didn't copy the else part so i missed it out, opps
0

why don't use jquery???, do something like this:

$("#li1").change(function(){
alert("it changed");//here do whatever you want to
});

I think it's easier and it's more read readable.(don't forget to import the Jquery library)

1 Comment

jQuery is certainly the modern, advanced way to go, but it's clear that the questioner is still learning the basics of JavaScript and HTML.
0

I did some tweaks to make changes ONLY when the text is "Firstname:" so repeated calls don't mess with the DOM unnecessarily (see the performance point 9 here)

JS:

function FormChange() {
    var obj = document.getElementById('li1');
    var oldHTML = obj.innerHTML.substring(0,10);

    if (oldHTML=="Firstname:") {
        var newHTML = "Company Name: <input type=\"text\" name=\"companyname\" />";
        obj.innerHTML = newHTML;
    }
}

HTML:

<form name="myForm" action="insert.php" method="post">
    <ul>
        <li id="li1">Firstname: <input name="firstname" type="text" /></li>
    </ul>
</form>
<input type="button" onclick="FormChange();" value="Change that bad boy" />

Demo

Further Notes:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.