1

I'm trying to let the user dynamically add languages with remove buttons for each one. I cannot figure out how to make the remove buttons work Basically, I want the input text field removed if the X glyphicon next to it pressed Thanks

        function setLanguage(){
              var l_name = document.getElementById("selected_language").value;
             var l_level = document.getElementById("lang_level").value;
       document.getElementById("sel_languages").innerHTML += "<div><input type = 'text'  readonly='readonly' name = 'language_selected1[]' value ='"  +  l_name  + " " + l_level  + "'/> <span id='language_clear' onclick='myFunction()'  class='glyphicon glyphicon-remove-circle'></span></div>" ;

     }




  function   myFunction(){
            $(this).parent().remove();

    }
1
  • We'll need to see the html, and let us know what you tried. Commented Jun 20, 2018 at 3:59

1 Answer 1

1

Instead of using the onclick attribute of the html element, you can add document.getElementById("language_clear").addEventListener("click", myFunction); to the following line. I've posted a simple working example here.

w3schools has a nice article on event listeners here. If you'd prefer a jQuery version, you may want to check the documentation here; they have some nice examples.

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

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.