susan
susan

Reputation: 382

Get name from one element and call to another element

Disclaimer:This is a unique situation and very hackish.

I have one set of radios that are visible to users and another set that is hidden. I need to pull the name from the hidden set and assign to the visible set.

Hidden radios:

<div class="productAttributeValue">
    <div class="productOptionViewRadio">
        <ul>
            <li>
                <label>
                    <input type="radio" class="validation" name="attribute[139]"
            value="86" checked="checked" />
                    <span class="name">Standard Shipping</span>
                </label>
            </li>
            etc etc...more li's
        </ul>
    </div>
</div>

A visible radio:

<label>
    <input type="radio" class="validation" name="PUT OTHER NAME HERE" value="86" checked="checked" />
        <span class="name">Standard Shipping</span>
            <p class="rl-m"><small>Earliest Date of Delivery:</small>
                <small><span id="delivery-date"></span></small></p>
</label>

So, in this case, I would like the name "attribute[139]" to somehow be gotten from the hidden radio and called to the name of the visible radio. I'm thinking of something like this:

<script>                    
    $(function() {
       var name = $(".productOptionViewRadio span.name:contains('(Standard)')").attr('name');                    
    });
</script>

I'm not too sure about the script being the right way to go about this and also not sure how I would actually get the value from the one element to populate to the other element's name field.

Thank you very much.

Update: Fiddle http://jsfiddle.net/susan999/XBcaF/

Upvotes: 0

Views: 85

Answers (3)

beautifulcoder
beautifulcoder

Reputation: 11320

Try

$('label input[type=radio]').attr('name',
  $('.productOptionViewRadio input[type=radio]').attr('name'));

http://jsfiddle.net/XBcaF/5/

Upvotes: 1

Pablo Anaya
Pablo Anaya

Reputation: 341

You can try something like this:

var checkboxCount = 0;
$("#visibleCheckboxes").find("input[type=checkbox]").each(function(){
    $($("#hiddenCheckboxes").find("input[type=checkbox]").get(checkboxCount)).attr('name', $(this).attr('name'));
    checkboxCount++;
});

I've prepared a Fiddle for you: http://jsfiddle.net/MJNeY/1/

Upvotes: 0

charlietfl
charlietfl

Reputation: 171679

Try this

$('input:radio.validation:visible').attr('name',function(){
  return $('input:radio.validation:hidden').attr('name')
})

Could improve the selectors if know more about parent classes of visible radios, or what elements are actualy being set as hidden

Upvotes: 1

Related Questions