0

I have this HTML code

    <h3></h3><br></div>
<div style="border-left: solid #0a0a0a 1px; float:left; height: 80%; margin-top:7%; width: 10px;" ></div>
<div style="width: 49%; float: right;">
    <h3></h3><br>
    <input name="jurors" id="jurors" type="text" style="width: 80%;">
    <br>
    <input type="button" value="Add" class="addButton">
    <br>
    <br>
    <br>
    <br>
    <select style="width:80%;height:250px; bottom: 0;" rows="10" multiple="multiple"></select>

And what I wana do with this is to get the value from the Input field by clicking the "add" button and add it to the select box. Also if posible to remove a value from the selct box by clicking on it. I need the select box values as a list to be inserted later into my DB. I beleve this could be done with jQuery (adding and removing the values) but my knowledge of the language is too basic to do it.

Thank you for your help in advance.

2
  • Show us your attempt. People will help you with your code, but not write it outright. Commented Mar 21, 2014 at 10:39
  • 3
    learn.jquery.com Commented Mar 21, 2014 at 10:40

1 Answer 1

2

I've come up with a Javascript/JQuery solution. Let's go through it so you know what's going on and if you need to change it you can. I've left your HTML pretty much the same (I've taken away some styling for the sake of development), anyway here it is:

    <h3>Adding and removing options</h3>
<input name="jurors" id="jurors" type="text" style="width: 80%;">
<br>
    <input type="button" value="Add" class="addButton" onclick="add();" /> <!-- The 'onclick="add();" will call a the Javascript function add() -->
    <br />
<select id="mySelect" style="width:80%;height:150px; bottom: 0;" rows="10" multiple="multiple"></select>

Here's the Javascript/JQuery:

function add(){ // Our function called by your button
    var x = $("#jurors").val(); // This is a JQuery function that'll get the value of a element with the ID 'jurors' - our textbox
    $("#jurors").val(""); // Clear the textbox value
    if(x==""){} // If there's nothing in the textbox
    else{
        $("#mySelect").append("<option value='"+x+"'>"+x+"</option>" ); // Get our select and add a option with the value of our textbox value with the text of the textbox input
    }
}

$("#mySelect").change(function(e){ // This is called whenever the user selects a option
    var x = e.target.value; // Get the value of the selected option
    $("#mySelect option[value='"+x+"']").remove(); // Remove the selected option
});

Here's the link to the JSFiddle: http://jsfiddle.net/gS2jS/2/

When you use this code in your website, there are a few things you'll have to remember; import JQuery, put the code in the body and the code must be placed after the inputs etc required, here's the HTML with the required code:

<html>
<head>
</head>
<body>
<h3>Adding and removing options</h3>
<input name="jurors" id="jurors" type="text" style="width: 80%;">
<br>
<input type="button" value="Add" class="addButton" onclick="add();" /> <!-- The 'onclick="add();" will call a the Javascript function add() -->
<br />
<select id="mySelect" style="width:80%;height:150px; bottom: 0;" rows="10" multiple="multiple"></select>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <!-- Import JQuery -->
<script>
function add(){ // Our function called by your button
    var x = $("#jurors").val(); // This is a JQuery function that'll get the value of a element with the ID 'jurors' - our textbox
    $("#jurors").val(""); // Clear the textbox value
    if(x==""){} // If there's nothing in the textbox
    else{
        $("#mySelect").append("<option value='"+x+"'>"+x+"</option>" ); // Get our select and add a option with the value of our textbox value with the text of the textbox input
        }
    }

$("#mySelect").change(function(e){ // This is called whenever the user selects a option
    var x = e.target.value; // Get the value of the selected option
    $("#mySelect option[value='"+x+"']").remove(); // Remove the selected option
});
</script>
</body>
</html>

That answer was longer than I hoped, sorry about that. Anyways, if you need any help, don't hesitate to leave a comment.

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

1 Comment

Thank you so much. Worked like a charm.

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.