3

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?

2
  • Aside from the fact that it's not valid to have a name property on a div, you have syntax errors: $(#divId) needs to be: $('#divId') Commented Jun 13, 2012 at 20:07
  • There are so many errors in that code it's ridiculous. Click the "JSLint" button and fix the errors, it will atleast get you closer. Commented Jun 13, 2012 at 20:08

5 Answers 5

4

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/

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

Comments

4

Instead of:

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

Use:

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

attr is a function not a property.

Comments

3

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.

Comments

2

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 $()

Comments

1

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

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.