0

I am very new to JQuery - I looked but could not find answers simple enough for my issues... I have HTML drop down list (generated by a db file) with an ID on a selection common to all lists. It is on this "Add a New" option that I would like to trigger an event that will immediately slide open a div that will allow input & update of a new name to the list.

<select name="NAME" id="NMDROP">
<option value=""></option>
<option value="JOE">Joe</option>
<option value="BOB">Bob</option>
<option id="ADDNM" value="ADD">Add a New Name</option>
</select>

This is the jQuery I am hoping will allow me to call an update pgm.

jQuery(document).ready(function () {

    //Jquery for Adding a SalesPerson
    $("#ADDNM").click(function () {
        if ($(this).is(":clicked")) {

            jQuery.ajax({
                url: "Update.pgm",
                type: "POST",
                data: {
                    "task": "ajax_addsp",
                    "ajax_add": 'Y',
                    "NAME": NAME
                },
                success: function () { alert("success:");
                },
                error: function () {  alert("error on Add a New Name " + data);
                }
            });
        }
    });
});

I am confident that many of you will be able to spot my fundamental errors. I am not even sure if this is a click or a change event, I have seen it both ways on this site.

7
  • Is the ID ADDNM unique? Commented Jun 12, 2013 at 19:19
  • @j08691 seems you are right, its a global attribute Commented Jun 12, 2013 at 19:43
  • @j08691 roasted have right. Option didn't have attribute id. Look: w3.org/TR/html-markup/option.html Commented Jun 12, 2013 at 19:46
  • @WooCaSh - no, roasted doesn't "have right". It's a valid attribute. Did you even read the page you linked to? It says right there that global attributes are valid on option elements, and the ID is a global attribute. Commented Jun 12, 2013 at 19:48
  • @j08691 Yes. you have right. My bad. Commented Jun 12, 2013 at 19:52

2 Answers 2

1

change your code to:

$("#NMDROP").on('change', function () {
    if ($(this).val() === 'ADD'){
        //rest of code
    }
});

DEMO

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

2 Comments

To try & see if I am detecting the event at all I used your code like this:
I'm sorry to be so thick - because no I do not have it working yet!!
0

Instead of using click, use the change event on your dropdown and check if the selected value is "ADD".

$("#NMDROP").on("change", function(e) {
    if($("#NMDROP").val() == "ADD") {
        //ajax method here
    }
});

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.