422
422

Reputation: 5770

display input field on new line ( based on radio select )

Ok found some great code on Stack. Have adapted it slightly.

But in essence we have a radio box group, in a form. And onclick of one of these radio buttons, an input field is displayed.

I need to show this input field on a new line ( preferably with a new label too ) but struggling.

The code is: html

<div class="s_row_2 clearfix">
        <label><strong>Price</strong></label>
            <div class="s_full">
                <label class="s_radio"><input type="radio" checked="checked" name="r" id="r1" onchange="disableTxt()"/> Free</label>
                <label class="s_radio"><input type="radio" name="r" id="r2" onchange="disableTxt()"/> Swap / Trade</label>
                <label class="s_radio"><input type="radio" name="r" id="r3" onchange="disableTxt()"/> Lets Talk</label>
                <label class="s_radio"><input type="radio" name="r" id="r3" onchange="enableTxt()"/> Price</label>
                    <input type="text" name="r" id="price" style="display:none;width:212px;" size="30" placeholder="Type your price"/>
            </div>
    </div>

js:

var temp = '';
    function disableTxt() {
        var field = document.getElementById("price");
        if(field.value != '') {
            temp = field.value;
        }
        field.style.display = "none";
        field.value = '';
    }
    function enableTxt() {
        document.getElementById("price").style.display = "inline";
        document.getElementById("price").value = temp;
    }

Fiddle: http://jsfiddle.net/ozzy/mnxRr/

Essentially, I would prefer price as the first option. So that onclick the input field displays below the radio selections with a new label.

Any suggestions please

to recap sorry:

I prefer html like so:

<div class="s_full">
                <label class="s_radio"><input type="radio" name="r" id="r3" onchange="enableTxt()"/> Price</label>
                    <input type="text" name="r" id="price" style="display:none;width:212px;" size="30" placeholder="Type your price"/>
                <label class="s_radio"><input type="radio" checked="checked" name="r" id="r1" onchange="disableTxt()"/> Free</label>
                <label class="s_radio"><input type="radio" name="r" id="r2" onchange="disableTxt()"/> Swap / Trade</label>
                <label class="s_radio"><input type="radio" name="r" id="r3" onchange="disableTxt()"/> Lets Talk</label>
            </div>

Upvotes: 0

Views: 985

Answers (1)

Julian D.
Julian D.

Reputation: 5463

Why don't you simply reorder your radio buttons?

<div class="s_full">
    <label class="s_radio"><input type="radio" name="r" id="r3" onchange="enableTxt()"/> Price</label>
    <label class="s_radio"><input type="radio" checked="checked" name="r" id="r1" onchange="disableTxt()"/> Free</label>
    <label class="s_radio"><input type="radio" name="r" id="r2" onchange="disableTxt()"/> Swap / Trade</label>
    <label class="s_radio"><input type="radio" name="r" id="r3" onchange="disableTxt()"/> Lets Talk</label><br />
    <input type="text" name="r" id="price" style="display:none;width:212px;" size="30" placeholder="Type your price"/>
</div>

seems to do the trick, doesn't it?

Upvotes: 1

Related Questions