views:

38

answers:

2

Hey guys, I've got what I imagine is a simple question, but for some reason, I can't get it working right.

Expected Behavior: I type characters into a field, and, through a function, those characters are translated into the value of the name HTML attribute.

Actual Behavior: Reflected in Firebug, the value doesn't change or update.

The Code:

$('input').live('keyup', function() {
    var name_value = $(this).val().toLowerCase();
    $(this).attr('name', name_value);
});

Just a side note: I'm using .live() because the element can be cloned, and those clones need to also be able to take on the properties of this .keyup event.

Any ideas?

A: 

in most cases you should put a die() function before live so it kills any previous function ... so your code should look like

$('input').die().live('keyup', function() {
        var name_value = $(this).val().toLowerCase();
        $(this).attr('name', name_value);
    });

hopw this does the trick for you .

Val
No, you shouldn't.
SLaks
Killing any possible events set by others is not nice.
Matti Virkkunen
actually it will only kill events of input ... otherwise i would recommend using a id so it does not do it to ALL input tags.$('#inputID')... then it will only kill any live first then create a new instance of live...
Val
+1  A: 

I have to agree with the comments that is is a particularly odd request, but that being said...what you have already works, it's the monitoring tools that aren't updating, not the DOM, it's working correctly.

The easy way to test this is to .serialize() the <form> and see what you get, to debug and see it, do this:

$("form").submit(function() {
    alert($(this).serialize());
    return false;
});

You can see a working demo here, works in Chrome, FireFox and IE8.

Nick Craver
If you have category 'a' that holds options 'foo1, foo2, foo3', category 'c' that holds options 'bar1, bar2, bar3' and so on, what would you recommend as the best way to bring those both through `$_POST` so that you can easily associate them on the other side?
dclowd9901
@dclowd9901 - I'd return an input with a comma separated list of names that go with the category, just number the inputs sequentially "input1", "input2" or something, on the PHP side you can just `.split(',')` that value and get the post values for that category. Though what you have works, just a bit odd :)
Nick Craver