1

I have multiple selects and would like to add or remove a name attribute depending on the option that is chosen in the first select.

Here is a fiddle for an example:

http://jsfiddle.net/Nirvanachain/DZFFe/

Mdd
  • 4,140
  • 12
  • 45
  • 70

4 Answers4

1

This should work:

$("#mySelect").change(function() {
    var x = $(this).val();

    if ($("#" + x).length > 0) {
        $("#" + x).attr("name", "myName");
    } else {
        $(".selectorClass").removeAttr("name");
    }

});​
Blender
  • 289,723
  • 53
  • 439
  • 496
  • Thanks for the quick response! It is not removing the name attribute though after changing #mySelect a second time. I am checking in firebug if that matters. – Mdd Mar 21 '12 at 02:04
1

You just had a few things backwards and you forgot the '.' in the class selector: $('.class'). I fixed the fiddle: http://jsfiddle.net/DZFFe/7/

$("#mySelect").change(function() {
    $(".selectorClass").removeAttr("name");
    var x = document.getElementById("mySelect").value;
    if($("#" + x)) {
        $("#" + x).attr("name", "myName");
    }
});

Milimetric
  • 13,411
  • 4
  • 44
  • 56
  • Thanks so much! That's exactly what I was looking for. I've been trying to get better with javascript and am still figuring out jquery. Thanks for the advice! – Mdd Mar 21 '12 at 02:29
  • Knowing jQuery is knowing CSS. Knowing CSS is understanding the difference between the id and class attributes, and learning how to use as few of them as humanly possible. Learning how to use as few of them as humanly possible is understanding the concept of cascading. Learning how to cascade takes a little practice. Hope this helps, good luck in your journey. – Milimetric Mar 21 '12 at 13:11
  • btw, I forget the `.` in the class selector all the time, it's such a pain in the @$$ :) – Milimetric Mar 21 '12 at 13:12
0

Something like the following?

$("#mySelect").change(function() {
    $('.selectorClass').removeAttr('name');
    $('#' + $(this).val()).attr('name', 'myName');                      
});

http://jsfiddle.net/DZFFe/8/

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
0

there were problems when !!x === false (ex: empty string in this case). I added a simple check on the if

I also added a missing . to select by class

$("#mySelect").change(function() {
    var x = document.getElementById("mySelect").value;
    if(x && $("#" + x)) { //added to check if x != ""
        $("#" + x).attr("name", "myName");
    } else {
        $(".selectorClass").removeAttr("name"); //added missing "." to actually select classes
    }
});

Here is a jsFiddle

ajax333221
  • 11,436
  • 16
  • 61
  • 95
  • Thanks Ajax. I accepted an answer but I didn't expect so many quick responses. I'm new to stackoverflow and am trying to get better with javascript but I wasn't sure where to turn when I ran into a wall. Thanks for the advice! – Mdd Mar 21 '12 at 02:33
  • @Mcd I fixed your code, although I doubt you want it to do what it does. It currently check if the element exist, if it exist it adds the name (even if there are others with the same name). And when it fails to find the element `(#x)`, it removes the names from all the elements with the calss `.selectorClass` – ajax333221 Mar 21 '12 at 02:42
  • I've just been trying to understand how to use different jquery functions. I recently was exposed to the .change() functon and have been trying to figure out how to use it. I thought i could use .attr() to add an attribute and then .removeAttr() to remove it. I see there's a .prop() jquery function too although I'm not sure when to use .prop() instead of .attr(). Thanks again for the quick responses and for sharing information on how to correct my mistake :) – Mdd Mar 21 '12 at 02:47