how to get the selected list attribute value - javascript

I have a created a list of options and i have could multiselect the list. all i want to know is how could i know which are the items from the list is selected. on click i m adding an attribute to the list.as below.

Since you have an attribute, use it along with attribute equals selector
var $selected = $('#user-role > li[data-role-selected="selected"]')

Related

Javascript 'select' on change go to anchor (anchor id is the select value)

I have my script creating a dropdown box (select) and populating the values with items from an array that I looped through. The values of these items are also the same as the id's for anchor points throughout the page. I'm trying to make it so when the select is changed, it goes to the anchor point that matches the value of the select.
error_anchor is an array of items where the values are also the same as the anchor id's
for (var i = 0; i < error_anchor.length; i++) {
var option = document.createElement("option");
option.setAttribute("value", error_anchor[i]);
option.text = error_anchor[I];
option.onChange="window.location.hash=this.value";
selectList.appendChild(option);
};
This doesn't throw an error but it doesn't do anything when I change the select value. I read online about using this.value and figured it would use the selected value, but perhaps it was just a placeholder in someone else's example?
I tried placing the onChange line outside of the loop, as I suppose it doesn't need to be in there, but it doesn't seem to have made a difference.
Would appreciate the help! Thanks
The event name is onchange not onChange and you should assign a function to it (not a string).
Also, the event should be on the select, not the options. this.value will contain the current chosen option value.
You can see a demo here

Delete Dropdown values using jquery

I have a situation here,
Using JQuery I'm appending some values in drop down list.. Even it is one or two value that appended in drop down list..
For Add,
tableui+='<option value="">'+resourceadd+'</option>';
$('#resourcess').append(tableui);
When the page reloads automatically the value stored in db.
For example 4 values added in dropdown list (Values are not stored in db), I want to delete last value. I'm using,
$("#resourcess :last-child").remove();
The same condition, I want to delete middle of two values, How to do it??
Assuming you know the value of the option you want to remove, you could use filter:
$('#resources option').filter(function() {
return this.value == 'foo'; // insert your value here.
}).remove();
Or an attribute selector:
$('#resources option[value="foo"]').remove();
If you don't know the value, but do know the position of the option within the select, you could remove it by index using eq():
$('#resources option').eq(1).remove(); // remove the 2nd option

Get selected index of dropdown from array Jquery

I am saving all inputs in a form in the form of array into a variable.
Example: var data = $('inputs,select')
Now, I want to get selected index of dropdown using variable data.
Please help.
Edit: Added Fiddle for reference
Fiddle
If I have understood your question properly, you save a jQuery object with every input and select in a variable­. To get the selected index of a dropdown you would have to iterate over your variable to find out if its a select or a regular input, and then get its selected index.
//loop over every dom element in the variable
data.each(function () {
//if its a select
if ($(this).is("select")) {
//find its selected index using native DOM and do something with it
$(this)[0].selectedIndex;
}
});
You probably want to add $( "select option:selected" ).text(); to select the selected item.
This was from: http://learn.jquery.com/using-jquery-core/faq/how-do-i-get-the-text-value-of-a-selected-option/

How to insert selected element from multiple selection into an input?

hi everyone i have a problem.
i have a multiple selection and i want to select something and put it into an input through a button i hope i have been clear :
i manage to get the select item with this jquery code :
var chosen= $('#droite option:selected').val();
droite is an id for the multiple selection
and i want to put it into the input wich has an id : chosen item here is my jquery code:
$("#chosenitem").prepend(chosen);
and it won't work do you have any idea why .?
You need to call val() on the select itself, not the options it contains:
var chosen = $('#droite').val();
Similarly, to set the value of the #chosenitem input, use val() with a parameter:
$("#chosenitem").val(chosen);
Note that if multiple options are selected in the #droite element, the value returned will be a comma delimited string, eg. foo,bar,baz.
You should call val() to set the value of #chosenitem
$("#chosenitem").val(chosen);

How to set option selected when using jqtransform on select element

How to set option selected when using jqtransform on select element.
I'm using jqtransform for my form , and I want to set appropriate option value to be selected when I'm retrieving saved data from database.
I believe you would set the value of select the same way as without using the jqTransform.
E.g.
$('#mySelect').val('myVal');
Edit:
Well, this is really ugly but should work:
var mySelect = $('#mySelect');
//find the current selected option
var myOption = mySelect.find('option[value='+mySelect.val()+']');
//find the index of that option
var index = $('#mySelect option').index(myOption);
//trigger the click on the corresponding jqTranfsform element
mySelect.prev('ul').find('li').eq(index).find('a').click();
You can call onchange event. jqtransform has change() method.
$('#mySelect').val('myVal').trigger('change');
It work's :)

Categories

Resources