1

I am getting whole select tag as a value from my code, in order to do work around the value i need to extract the value from my select tag,as this tag is dynamically created by the code.

Below is the value i am getting. How can i extract this using java script.Thanks for your help.

   rowId[0].QValue = "<select id="Type112" class="GridList" rownumber="0" value="Q1 Only" ><option></option><option value="1" selected="selected">Q1 Only</option><option value="2">Q2 Only</option></select>"

7
  • document.getElementById('Type112').value; should do! Commented Jan 24, 2017 at 4:52
  • @Rayon - As this id is dynamically generating, have tried but didnt find a way to gt that id. Commented Jan 24, 2017 at 4:59
  • @Virat What do you mean by dynamically generated? Is this generated due to user input? During rendering of you webpage? Commented Jan 24, 2017 at 5:00
  • As this id is dynamically generating not in the code you posted. Perhaps, seeing as the code you posted isn't valid javascript, you could help us help you by showing the code involved - Below is the value i am getting how are you getting this? And how is the code you posted related to valid javascript code? Commented Jan 24, 2017 at 5:00
  • Maybe a duplicate of stackoverflow.com/q/1085801/6320039 Commented Jan 24, 2017 at 5:02

3 Answers 3

2

The proper way to do this would be to select the element from the DOM with one of the selection functions. In this case, I prefer document.querySelector:

var type112 = document.querySelector('#type112');

The # means 'id', and you can pass any combination of valid CSS to document.querySelector.

Then, to produce the value of this element, simply call

type112.value

This will give you the text value of the currently selected option within the select element.

Based on your comment, I'm sensing that perhaps you have the text of an element and want to parse out the id? If that's the case, you can try:

var elemString = // whatever your str is
var id = (elemString.match(/id="([^"]+)"/) || [])[0];

This assumes that the id is the first attribute in the string, as well as a whole litany of other things that will probably break in production but will work in the absence of a coherent understanding of what you're trying to do.

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

6 Comments

OP asked for value, you are returning the element
Even this Id is generating dynamically, for different values have diff ids. Is there any way to split the code and extract value?
@Virat Updated the answer. Are you saying that you have a string, or that you have an HTMLElement?
@furkle I have mentioned the value as "rowId[0].QValue = <select". It is a string type. sorry for not displayed clearly. The value should be like this rowId[0].QValue = "<select......./>"
@furkle, the same way can i get the value, like your answer parse out the id?
|
2

You can simply use the select element id to retrieve the value of the element.

   <select id="Type112" class="GridList" rownumber="0" value="Q1 Only" ><option></option><option value="1" selected="selected">Q1 Only</option><option value="2">Q2 Only</option></select>

You can write the javascript to get the element by id Type112 and so on to get the value:

var s = document.getElementById("Type112");
var selNum = s.options[s.selectedIndex].value;
alert(selNum);

Here's a jsfiddle example

Comments

0

Try this.

var list = document.getElementById("Type112");
console.log(list.value)
<select id="Type112" class="GridList" rownumber="0" value="Q1 Only" ><option></option><option value="1" selected="selected">Q1 Only</option><option value="2">Q2 Only</option></select>

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.