How can I have a drop down box where you pick something and it sets the HTML tag set to what the drop down is set on?</a></h1> </div> <div class="grid fw-wrap pb8 mb16 bb bc-black-075"> <div class="grid--cell ws-nowrap mr16 mb8" title="2016-01-12 19:07:53Z"> <span class="fc-light mr2">Asked</span> <time itemprop="dateCreated" datetime="2019-11-06T03:08:13.280" class="fromnow">Nov 06 '19 at 03:08</time> </div> <div class="grid--cell ws-nowrap mr16 mb8"> <span class="fc-light mr2">Active</span> <time class="fromnow" title="2019-11-06T03:52:56.997" datetime="2019-11-06T03:52:56.997">Nov 06 '19 at 03:52</a> </div> <div class="grid--cell ws-nowrap mb8" title="Viewed 111 times"> <span class="fc-light mr2">Viewed</span> 111 times </div> </div> <div id="mainbar" role="main" aria-label="questions and answers"> <div id="question" class="question" data-questionid="58722534" data-ownerid="12329610" data-score="2"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="58722534"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="2">2</div> <button class="js-bookmark-btn s-btn s-btn__unset c-pointer py4"> <svg aria-hidden="true" class="svg-icon iconBookmark" width="18" height="18" viewBox="0 0 18 18"><path d="M6 1a2 2 0 00-2 2v14l5-4 5 4V3a2 2 0 00-2-2H6zm3.9 3.83h2.9l-2.35 1.7.9 2.77L9 7.59l-2.35 1.7.9-2.76-2.35-1.7h2.9L9 2.06l.9 2.77z"></path></svg> <div class="js-bookmark-count mt4" data-value=""></div> </button> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>This is what I have but I don't know how to get it to a variable in JS</p> <pre><code><select> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> <button onclick="myFunction()">set</button> <script> function myFunction() { var x = document.title; } </script> </code></pre></div> <div class="mt24 mb12"> <div class="post-taglist grid gs4 gsy fd-column"> <div class="grid ps-relative"> <a href="../../questions/tagged/html" class="post-tag js-gps-track" title="show questions tagged 'html'" rel="tag">html</a> <a href="../../questions/tagged/page-title" class="post-tag js-gps-track" title="show questions tagged 'page-title'" rel="tag">page-title</a> </div> </div> </div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature owner grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="asked Nov 06 '19 at 03:08">asked Nov 06 '19 at 03:08</time> <a href="../../users/12329610/meponder" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/12329610.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="meponder " /> </a> <div class="s-user-card--info"> <a href="../../users/12329610/meponder" class="s-user-card--link">meponder </a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">21</li> <li class="s-award-bling s-award-bling__bronze" title="5 bronze badges">5</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-58722534" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="58722534" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-103737498" class="comment js-comment " data-comment-id="103737498" data-comment-owner-id="11700321" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment103737498_58722534"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Could you clarify what you mean by `HTML <title> tag set to what the drop down is set on?` ? Do you want it so when you hover over option with `value="1"` that the title object is set to `1` and so on so for down to `option` where `value="4"`? – EGC Nov 06 '19 at 03:20

2 Answers2

2

In this code you are storing current title in var x. you need to write like this:

document.title="Some Text or variable";

if you need to set value of dropdown item as title then to give id to select tag then var newtitle = document.getElementById("id_of_selectTag").value; document.title=newtitle;

0

There is already answers on StackOverflow: Get selected value in dropdown list using JavaScript and How to get the title of HTML page with JavaScript. You can set title assigning the desired value to document.title variable.

Valentine Shi
  • 6,604
  • 4
  • 46
  • 46