larin555
larin555

Reputation: 1699

Change NAME attribute onClick with Jquery

I am trying to change the NAME attribute of a DIV for the content of a textbox using jQuery.

Here's my fiddle: http://jsfiddle.net/e6kCH/

What am I doing wrong?

Upvotes: 3

Views: 9422

Answers (5)

user1454661
user1454661

Reputation:

Keep the same code, only change the JS for this:

$('#buttonId').click(function() {
    $('#divId').attr('name', $('#textId').val());
});

Keep in mind it only changes the name of the div, you'll need something like Google Chrome's developer tools to see it change.

Upvotes: 3

frosty
frosty

Reputation: 21762

http://jsfiddle.net/e6kCH/10/ That should get you what you want.

Upvotes: 1

Kevin B
Kevin B

Reputation: 95023

The biggest problem is a logic problem.

content = document.getElementById("theid").value

This gets the current value of the input. The problem is, it does not get updated when you change the value of the input. To solve that, move the line into the click event handler.

Once the various other javascript errors are fixed (Click the JSLint button!!!), it will work. http://jsfiddle.net/e6kCH/4/

Upvotes: 4

Didier Ghys
Didier Ghys

Reputation: 30666

The way to use .attr() to set the value of the attribute is:

.attr( attributeName, value )
Description: Set one or more attributes for the set of matched elements.

attributeName: the name of the attribute to set.
value: a value to set for the attribute.

$('#divId').attr("name", content);

Note: don't forget to surround your selectors with quotes (or double quotes) within the jQuery function $()

Upvotes: 2

Sarfraz
Sarfraz

Reputation: 382686

Instead of:

$(#divId).attr("name")=content;

Use:

$("#divId").attr("name", content);

attr is a function not a property.

Upvotes: 4

Related Questions