0

I need to somehow have data-rel value to input field and remove name from same input field on specific class.

The code i had done so far is working to remove name from that specific field. but i can't add data-rel to that same field.

Here is the code.

$('.vf-sortable-holder').each(function() {
    var empty_sortable = $(this).closest('.option').find('.empty-sortable').find('input');
    var sortable_name = empty_sortable.attr('name');

    input.attr('data-rel', sortable_name);

    empty_sortable.removeAttr('name');

});

So html look like this

<div class="option">
    <div class="vf-sortable-holder">
        <div class="empty-sortable hidden">
        <input type="text" name="example">
        </div>
    </div>
</div>

<div class="option">
    <div class="vf-sortable-holder">
        <div class="empty-sortable hidden">
        <input type="text" name="example">
        </div>
    </div>
</div>

So my js code works to remove name attribute but i actually need to change name with data-rel either to remove name from html with js code and add data-rel or somehow to rename name to data-rel in the end i need it to look like this:

<div class="option">
    <div class="vf-sortable-holder">
        <div class="empty-sortable hidden">
        <input type="text" data-rel="example">
        </div>
    </div>
</div>

<div class="option">
    <div class="vf-sortable-holder">
        <div class="empty-sortable hidden">
        <input type="text" data-rel="example">
        </div>
    </div>
</div>
3
  • 2
    there is no empty-sortable element Commented Dec 12, 2013 at 15:27
  • yes, thanks for noticing, in the code there is empty-sortable, i just forgot to write it here in example,i edited page to add empty-sortable element. Commented Dec 12, 2013 at 15:31
  • what is "input"? That variable is not defined anywhere Commented Dec 12, 2013 at 15:34

3 Answers 3

3

Is this what you are looking for?

$('.vf-sortable-holder').each(function() {
    var empty_sortable = $(this).find('input');
    var sortable_name = empty_sortable.attr('name');

    empty_sortable.attr('data-rel', sortable_name).removeAttr('name');;
});

Also remove the '.' from your vf-sortable class name in the HTML.

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

2 Comments

was going to give the same answer, no need to go up & then down, when you have $(this)
Yes this did the trick, i just only had to change empty_sortable like you posted in your example and it worked, although i don't understand how this makes difference from what i have in my code, but this one works, thanks alot.
1

Selecting .vf-sortable-holder, then the parent .option, then the .empty-sortable and finally the input, all inside loops, seems like jumping through a lot of hoops ?

$('.option .vf-sortable-holder .empty-sortable input').attr('data-rel', function() {
    return this.name;
}).prop('name','');

Comments

0

Remove the . in the class name(.vf-sortable-holder)

<div class="option">
    <div class="vf-sortable-holder">
        <input type="text" name="example" />
    </div>
</div>
<div class="option">
    <div class="vf-sortable-holder">
        <input type="text" name="example2" />
    </div>
</div>

then

jQuery(function () {
    $('.vf-sortable-holder input').attr('data-rel', function () {
        var name = this.name;
        $(this).removeAttr('name');
        return name;
    })
})

Demo: Fiddle

Comments

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.