最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Extract value from html select tag using JavaScript - Stack Overflow

programmeradmin2浏览0评论

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>"

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>"

Share Improve this question edited Jan 24, 2017 at 5:23 Virat asked Jan 24, 2017 at 4:49 ViratVirat 1211 gold badge3 silver badges16 bronze badges 7
  • document.getElementById('Type112').value; should do! – Rayon 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. – Virat 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? – Ulysse BN 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? – Jaromanda X Commented Jan 24, 2017 at 5:00
  • Maybe a duplicate of stackoverflow./q/1085801/6320039 – Ulysse BN Commented Jan 24, 2017 at 5:02
 |  Show 2 more ments

3 Answers 3

Reset to default 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 bination 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 ment, 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.

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

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>

发布评论

评论列表(0)

  1. 暂无评论