jQuery: The Write Less, Do More JavaScript Library

"Swapping" attributes with attr

View: New views
3 Messages — Rating Filter:   Alert me  

"Swapping" attributes with attr

by Hanpan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi, I am trying to swap two attributes around, I am trying to achieve something like this:

$(trigger).find('input').attr({value: img_id, name: $(this).attr('id')});

As you can see, I am trying to swap the value of the name attribute with the value of the id attribute. The above code does not work, as $(this) is not input field object.

Can anyone suggest a clean way in which I can do the above?

Re: "Swapping" attributes with attr

by Charlie Tomlinson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

assuming $(trigger) is a valid selector, this should work:

var theValue = $(trigger).find('input').val();
$(trigger).find('input').attr('id',theValue');



Sceneshift wrote:
Hi, I am trying to swap two attributes around, I am trying to achieve
something like this:

$(trigger).find('input').attr({value: img_id, name: $(this).attr('id')});

As you can see, I am trying to swap the value of the name attribute with the
value of the id attribute. The above code does not work, as $(this) is not
input field object. 

Can anyone suggest a clean way in which I can do the above?
  


Re: "Swapping" attributes with attr

by ricardobeat :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


attr() accepts a function as the second argument:

$(trigger).find('input').val(img_id).attr('name', function(){ return
this.id });

On Jul 2, 11:45 am, Sceneshift <j...@...> wrote:

> Hi, I am trying to swap two attributes around, I am trying to achieve
> something like this:
>
> $(trigger).find('input').attr({value: img_id, name: $(this).attr('id')});
>
> As you can see, I am trying to swap the value of the name attribute with the
> value of the id attribute. The above code does not work, as $(this) is not
> input field object.
>
> Can anyone suggest a clean way in which I can do the above?
> --
> View this message in context:http://www.nabble.com/%22Swapping%22-attributes-with-attr-tp24308061s...
> Sent from the jQuery General Discussion mailing list archive at Nabble.com.