1

I have a simple website (pure HTML, CSS and JS), but want to use variables in this site.

Like: onclick="myFunction()" class="dropbtn">**$VARIABLE**

I want the text be variable. So VARIABLE=ABC or VARIABLE=123. This has to be shown in the dropdownbutton.

The same for:
href="index.html">$VARIABLE. Which shows the variable in the reference link.
or
input type="text" placeholder=**$VARIABLE** id="myInput" onkeyup="filterFunction()". Which shows the variable in the button.
or
href="" title=**$VARIABLE** ><img src="image/WhatsApp.jpg" alt="". Which shows the variable in the picture.
And more of this things.
Can not use JSP, because I use a IIS webserver (no servlets).

So, can it be done, can I use variable (like the examples) in my HTML with JavaScript (JS).

19
  • 1
    Sounds like you need a server side technology. ASP.net works with IIS or IIS can be configured to server PHP. You can't use variables in HTML as HTML is purely descriptive. You can use javascript to manipulate HTML, but from the sounds of it you will need more than basic javascript variables. Commented Nov 9, 2020 at 0:35
  • read developer.mozilla.org/en-US/docs/Learn/HTML/Howto/… Commented Nov 9, 2020 at 0:43
  • 1
    @MisterJojo Agreed, but he isn't able to get the data there in the first place. Commented Nov 9, 2020 at 3:03
  • 1
    For me, the important question is "where do these variables come from and how are you setting them?" Commented Nov 9, 2020 at 3:12
  • 1
    Do a Before/ After example (with explanations) ... Commented Nov 9, 2020 at 4:37

1 Answer 1

0

you mean that ?

const countrySelect = document.getElementsByClassName('dropbtn')[0]

countrySelect.onchange = () =>
  {
  let optElement = countrySelect.querySelector(`option[value="${countrySelect.value}"]`)
  console.clear()
  console.log('selected value = ', countrySelect.value )
  console.log('selected text  = ', optElement.textContent )
  }
<select name="dropbtn" id="ref_dropbtn" class="dropbtn" >
  <option value="" selected disabled>pick one:</option>
  <option value="England">England text</option>
  <option value="France">France text</option>
  <option value="Spain">Spain text</option>
</select>

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

11 Comments

No see my examples. Not class, variable
@HansvanderRijst Man, I try to help you, ask yourself why nobody here seem to understand what you're looking for
Sorry about the miscom. Suppose I have a variable: var $COUNTRY="England". Now I want to see this in for instance a dropdownbox. Like: onclick="myFunction()" class="dropbtn">$COUNTRY. So when I run the page, and I click on the dropdown I see "England". Just want to replace the hardcoded fixed text by a variable. In my example $COUNTRY. just replace fixed text, by a variable, Question is: How do I do it. What is the syntax? Because onclick="myFunction()" class="dropbtn">$COUNTRY, is not working, then I see when I run the page still "$COUNTRY" instead of "England". Thanks
@HansvanderRijst In witch context does your $COUNTRY variable exist? is it on the server or on the browser / client workstation? why add the $ sign to it?, javascript variables don't need to have this sign, unless you are using jQuery or another JS library like Angular. in other words, what is the context of this variable? what do you call a dropBox? there is no dropBox element in HTML, are you talking about a Select HTML? or is it an HTML reception area for a drag & drop?
@HansvanderRijst If it's a select, using a click event to change an option doesn't make sense, because every time the user wants to use that select, it will change their option each time. I changed my example by showing you the use of a button
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.