1

I have multiple dropdown menus that are the same, I need them to be the same.

 <select name="Obrojgoluba" id="otacmajka" class="selectbox_dodajpostojeceg" onchange="dodajpostojeceg('otac')">
  <option value="1">06557-07-681</option>
  <option value="2">07216-05-552</option>
</select>

This is my jQuery function to get value from that select list

function dodajpostojeceg(x)
{
    var ID = $('select[name="Obrojgoluba"]').val();
    alert(ID);

}

But problem is that it gets value from the first select list, not from others I have on my site. When I click on the first one it gets good values. but when I click on other select list on my site that are the same as first one it returns or empty or value it remembered from the first select list

0

3 Answers 3

2

You are using this

var ID = $('select[name="Obrojgoluba"]').val();

And it is possible as you said that there are multiple select on your page. So if they are having same name, above code would select the first one and that is what you are getting.

So you can just change the function and markup to made this code workable.

function dodajpostojeceg(element)
{
    var ID = $(element).val();
    alert(ID);
}

and call this function as like this

<select name="Obrojgoluba" id="otacmajka" class="selectbox_dodajpostojeceg" onchange="dodajpostojeceg(this)">

JS Fiddle Demo

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

Comments

1

A strange thing to be doing, but try this:

function dodajpostojeceg(x)
{
    var ID = $(this).val();
    alert(ID);
}

As CBRoe comments, multiple identical IDs are invalid, and this may hamper your progress...

1 Comment

ID is not relevant here cause I'm using it just to trick something. I'm not using it to identify something important
1

Make sure that the jquery lib is included and the function is defined before the html

<head>
    function dodajpostojeceg(x)
    {
        var ID = $('select[name="Obrojgoluba"]').val();
        alert(ID);
    }
</head>

<body>
    <select name="Obrojgoluba" id="otacmajka" class="selectbox_dodajpostojeceg" onchange="dodajpostojeceg('otac')">
        <option value="1">06557-07-681</option>
        <option value="2">07216-05-552</option>
    </select>
</body>

Fiddle: http://jsfiddle.net/kR8yH/

When the html is rendered, it will look for a function named dodajpostojeceg() and if it not defined, it will not be able to bind the select and the function

I am assuming that all the selects are uniquely named

1 Comment

not working cause there is multiple select on my websites that are exactly the same. But I found answer. thanks

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.