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

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;

Related

Store [value] contents from browser element as a string [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Get value of hidden input, send to another hidden input
(3 answers)
Closed 4 years ago.
I have a quick question regarding being able to store parts of an element as a string which I can later recall.
Example:
I have an element that looks like this
<input type= "hidden" name="Posted" id="Posted" value="100|307|151|16">
I can retrieve the element with document.getElementById('#Posted') and now I want to be able to take the contents in the [value] tag and store them as a string in a new variable, so I can get something like this:
var inputValue = "100|307|151|16"
Which I can later recall in my code.
Maybe I'm blind but after a bit of searching I've still come up with nothing. Thanks in advance for the help!
There's two different approaches you can take here.
You can either get the value of the hidden input, or you can get the value of the "value" attribute on the hidden input.
Here's how to do both:
var element = document.getElementById('Posted');
var value = element.value;
var attribute = element.getAttribute("value");
console.log(`value: ${value}`);
console.log(`attribute: ${attribute}`);
And here's a JSFiddle of that in action.
//If all you care about is the value, you can just use .value to get the information you want
const inputValue = document.getElementById('#Posted').value;
Have you tried:
var inputValue = document.getElementById('#Posted').getAttribute("value");

reading selected value from dropdown [duplicate]

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;

Attribute value as variable name [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 7 years ago.
I'm trying to get a dynamic attribute value to be used as a var name
What I have:
$(this).html(""+$(this).attr("id")+"");
It gives me the value as plain text. What I want is that it uses the attr("id") value as a Var like:
$(this).html(""+myVar+"")
Final example:
<div class="text" id="text1"></div>
<div class="text" id="text2"></div>
var text1 = "hello world"
var text2 = "bye world"
$(document).ready(function () {
$(".text").click(function(){
$(this).html(""+$(this).attr("id")+"");
});
});
Basically I want a dynamic way of calling to certain id's and getting their attribute value to be used as the Var name instead of plain text.
It all depends on variable namespace and scope, but generally next code should do the job.
$(".text").click(function(){
var varFromAttr = window[$(this).attr("id")];
$(this).html(varFromAttr);
});
Question is almost the same as this question.

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();

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