2

So I have this code. And I want to accomplish something probably simple for javascript guys. If a certain option is selected then add an input field on a div.

<select name="amount" >
    <option value="50">50$</option>
    <option value="100">100$</option>
    <option value="Other">Other</option>
</select>

<div id="custom_price">

</div>

So I want to add the input field once the 'Other' option is selected. I know there's a simple way around this but I cannot make it work:

document.getElementById('custom_price').innerHTML += "<input type="text" name="amount"/>"

So custom_div would look like:

<div id="custom_price">
Custom price:<br>
<input type="text" name="amount"/>
</div>
2
  • 4
    Hint: Look at the syntax highlighting. Commented Aug 14, 2012 at 19:16
  • @SLaks - Ah yes, there is a syntactical error there! All the posted answers (including mine) are just different approaches to do the same thing the OP already has psuedo-accomplished :) Commented Aug 14, 2012 at 19:22

5 Answers 5

6

Best way would be to actually make the dom element.

var newInput = document.createElement("input");
newInput.type="text";
newInput.name="amount";
document.getElementById('custom_price').appendChild(newInput);

Perhaps with a handler like this:

assign your select an id: <select name="amount" id="amount">

$("#amount").change( function() {
 if( $(this).val() == "Other" ){
  document.getElementById('custom_price').innerHTML = "";
  var newInput = document.createElement("input");
  newInput.type="text";
  newInput.name="amount";
  document.getElementById('custom_price').appendChild(newInput);
 }
});
Sign up to request clarification or add additional context in comments.

Comments

3

Judging by tags of your question you're using jQuery. So adding an input is fairly easy.

$("select[name=amount]").change(function() {
    if ($(this).val() == 'Other') {
        $("#custom_price").append('<input type="text" name="amount"/>');
    }
})

6 Comments

Thanks for your code. But when I select Other nothing appears... ?
Superfluous jQuery: how about just if (this.value=='Other') ...
This will create new input field every time client selects "Other" option. Use ".empty()" before append.
It works now. As I said I'm a dumb at js, I put the code before the form that's why It didnt worked in the beginning. Thank you alot.
@bornie check out $(document).ready(function(){ //code }); It lets you run code after the document has been loaded.
|
1

It's

document.getElementById('custom_price').innerHTML += '<input type="text" name="amount" />';

If you start a sring using ", you have to scape the caracters " inside it (\") or use '

Comments

0

Using jQuery

$("#custom_price").empty().append('Custom price:<br><input type="text" name="amount"/>')

Comments

0

Just place the input inside of a div and hide div initially. Then use jQuery/JS to show this div when necessary.

$('select[name="amount"]').change(function() {
    if($(this).val() == 'Other') {
        $('#custom_price').show();
    } else {
        $('#custom_price').hide();
    }
});

<div id="custom_price" style="display: none;">
    <p>Custom price:</p>
    <input type="text" name="amount"/>
</div>

5 Comments

Thank you but I'm not looking to hide it. I want it to be generated once the user selects a certain value. This is important in my case.
May i ask then why exactly do you need to create element on the fly? It's not about answering the exact question, but about implementing things the right (better) way.
Look the problem is with a paypal form. I have the select options that correspond to $ amounts. Also the custom payment field. But since paypal doesnt allow user defined variable I must name select and the custom amount variable the same > name="amount". In cases when user selects an option the other custom amount variable is still present (below) therefore when redirected to paypal the amount is 0 even when user selects 100$ from the menu
ok, got it. I would have created a proxy form for this with hidden fields. You can update field values with javascript when necessary and send this proxy form instead of visible one. This way you will have lot's of flexibility.
I will google that. Thanks for letting me know, I wasnt aware of such method.

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.