0

Given the following code:

<select name='starttime' onChange='cells(this);'><option value='1'>2004Q1</option><option value='2'>2004Q2</option></select> <select name='endtime' onChange='cells(this);'><option value='48'>2015Q4</option><option value='47'>2015Q3</option></select> 

How can I access the text of say, the option with value = 1 of the "starttime" <select> (i.e. 2004Q1)? I can access the value of this element by using the following jQuery code:

$('[name=starttime]').val() 

However, I'd like to instead specify a value and name, and get the associated text.

4
  • What do you mean by "label"? No <label> element appears at html at Question. Commented May 16, 2017 at 1:06
  • I apologize, I mean the text associated with an option. (i.e. the "label") Also you can ignore the word <div>, I just mean the <select>. Commented May 16, 2017 at 1:18
  • .textContent is not a "label" that am aware of. Can you link to documentation of <option> .textContent referenced as "the "label""? Commented May 16, 2017 at 1:21
  • I am using the word label as a colloquial term. Commented May 16, 2017 at 1:23

1 Answer 1

1

Is this what you need?

console.log($('[name="starttime"] [value="1"]').text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="starttime"> <option value="1">2004Q1</option> <option value="2">2004Q2</option> </select> <select name="endtime"> <option value="48">2015Q4</option> <option value="47">2015Q3</option> </select>

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

10 Comments

Note, cells function is not defined at stacksnippets
To be honest, I just answered to the specific point raised in the question. I did not refactor the HTML code which is completely invalid. <select> elements are not even closed, by the way...
Well, at least you are honest. How will a partially complete Answer help OP completely as to the Question? Though granted, the Question is incomplete.
It's 03:21 AM in France, I'm a bit tired... ^^ However, I modified my answer to include a clean HTML code.
Sorry, I did not end the select tag because I only copied a portion of the code!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.