reading selected value from dropdown [duplicate] - javascript

This question already has answers here:
Get selected value in dropdown list using JavaScript
(32 answers)
Closed 5 years ago.
i am trying to read selected value from dropdown list( ddDocTypes) . So in the IE I am able to get value using Form1.ddDocTypes.options(Form1.ddDocTypes.selectedIndex).innerText. But the same code give me "Form1.ddDocTypes.options undefined" error in Chrome.
Any suggestions !!

I don't know if you are just trying to get the value of the selected from the dropdown, but this is how you do it in plain javascript (guessing from your tag)
var e = document.getElementById("ddDocTypes");
var inner = e.options[e.selectedIndex].text;
var value = e.options[e.selectedIndex].value;

Related

how to get value by class name using javascript [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 7 years ago.
Sorry, it's basic one but I trying to search on google anyways but still not get success.
I want get value of this
<input type='hidden' class='hid_id' value='1' />
Using Javascript I want alert value 1
I trying this
var id = document.getElementsByClassName("hid_id");
alert (id);
But it's alert [object HTMLInputElement]
please help me now.
getElementsByClassName() returns an array so you have to access first element (if there is any). Then try accessing value property:
var id = document.getElementsByClassName("hid_id");
if (id.length > 0) {
alert (id[0].value);
}
jsfiddle
try this:
var id = document.getElementsByClassName("hid_id")[0].value;

How to take a value from a link using jquery [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 7 years ago.
I want to pass one value through ajax by taking the values from jQuery. But I am using link so I have problems taking the value. I tried the following,
<a id="addpa" class="ActionPopup" href="http://localhost:49951/admin/assignhome/Add?sPId=7">Add</a>
Jquery Code:
var spId = $("#addpa").prop("href"); // Here i am getting a whole Url
var thequerystring = getParameterByName("sPId");
The result is showing undefined. How to take the value of sPId? Give me ideas..
How to take the value of sPId?
Try using String.prototype.split() , Array.prototype.pop()
var spId = $("#addpa").prop("href").split(/=/).pop();

how to view the answer of a js function in a html input? [duplicate]

This question already has answers here:
Set the value of an input field
(17 answers)
Closed 7 years ago.
I've always been wondering this, but all websites seem to be answering the opposite (how to make text typed in an input be the input of a function).
Is there a way to do it?
Is this what you want to do?
function myFunction() {
// some functionality here
// this function must return something
}
var input = document.querySelector('input');
input.value = myFunction();
This will set the value of the input element to the value returned by myFunction.

Select dropdownmenu selected item [duplicate]

This question already has answers here:
How can I set the value of a DropDownList using jQuery?
(17 answers)
Closed 8 years ago.
I was wondering if anyone could advise me a little. I am attempting to use JQuery to select my dropdown item by a given value. Here is what I have
var myVar = document.getElementById('<%= hiddenID.ClientID %>').value
if (myVar != "") {
var val = $("select[name*=drpDescription] option").filter(function () {
return ($(this).val() == myVar)
});
$("select[name*=drpDescription]").val(val.val());
}
The above code snippet does get the correct option based on it's value matching my hiddenfield value. What I can't do is get my dropdown to display it as the selected item. I wondered if anoyone has any advise on where I am slightly missing the point.
Thank you for any help.
You can set the value by passing it as parameter in .val() with select elements selector:
$("select[name*=drpDescription]").val(myVar);

Jquery selecting element by 'data-id' [duplicate]

This question already has answers here:
jQuery how to find an element based on a data-attribute value?
(9 answers)
Closed 8 years ago.
Similar to the question here , I need to get an element based on its data-id.
The problem I encounter is that I want to retrieve a an a element with a certain data-id using a variable.
lets consider the code below:
var item_id = 12345;
if I want to get the element that has the data-id with the value of item_id, would I do the following ?
$('[data-id=item_id]')
This does not work, and I am kind of stuck on how I can get around this.
You can concatenate the variable using +:
$('[data-id=' + item_id + ']')

Categories

Resources