1

I found this neat code online that does exactly what I need. The problem I'm having is that the variable is not passing the entire value.

<script type='text/javascript'>
    $(window).load(function() {
        $("#add_id").click(function() {
            var value = $("#entered_id").val();
            $("#list_id").append("<option value =" + value + " selected>" + value + "</option>");
        });
    });    
</script>




<select id='entered_id'>
    <option value='This is a test message'>test</option>";
</select>


<button id="add_id">Add</button>


<h2>Entered IDs</h2>
<form name="test" method="POST" action="test2.php">
    <select id="list_id" name="list_id" multiple="multiple"> </select>
    <input type="submit" />
</form>

On test2.php the variable $list_id comes in as "This" instead of "This is a test".

Can anyone steer me in the right direction?

1 Answer 1

5

You should put the string values arround quotes ' :

$("#list_id").append("<option value =" + value + " selected>" + value + "</option>");

Should be :

$("#list_id").append("<option value ='" + value + "' selected>" + value + "</option>");
_____________________________________^_____________^

In other words, the generated option html looked like <option value=This is a test>, making is, a, and test invalid/unknown attributes.

Hope this helps.

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

4 Comments

In other words, the generated option html looked like <option value=This is a test>, making is, a, and test invalid/unknown attributes.
Thanks @MarcB for your intervention, can i update my answer using your explanation?
of course... you got the right answer, this just expands on WHY it's the right anwer.
If the value happens to include a double-quote, he'll have the same problem again. He should add some code to replace " with &quot; inside the string.

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.