3

I am trying to add few items to a list box from a text box using javascript. but the problem is as soon as I am adding the items, it gets visible in the list box for a second and then gets deleted. Any solution for this?

I am adding items from TextBox4 to ListBox1

function AddToList() {

    // Create an Option object        

    var opt = document.createElement("option");

    // Add an Option object to List Box

    document.getElementById("ListBox1").options.add(opt);
    opt.text = document.getElementById("TextBox4").value;
    opt.value = document.getElementById("TextBox4").value;
    document.getElementById("ListBox1").options.add(opt);


}

2 Answers 2

5
document.getElementById("ListBox1").options.add(opt); // remove this line
opt.text = document.getElementById("TextBox4").value;
opt.value = document.getElementById("TextBox4").value;
document.getElementById("ListBox1").options.add(opt); 

As you're appending an <option> which has no value or text.

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

1 Comment

Still not working. Same thing is happening, items are getting added and within a second they are disappearing.
0

Got the solution, Need to add

return false;

at the end of the function.

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.