0

Can I add JS in this code? My point is that when I click the button, it will find the car and open the link assigned to it.

<select class="selecttype" id="cars">
  <optgroup label="RENAULT">
  <option id="euro" href="mylink" value="euro1">Renault Euro 3</option>
  <option id="euro" href="mylink" value="euro2">Renault Euro 4</option>
</select>

<button class="buttonsea" type="submit" onclick="btnclick">Search</button>

1
  • Yes, you can, you can receive and display information from the server by ajax or other ways but your question is incomplete and need to more detail. Commented Nov 16, 2020 at 8:00

6 Answers 6

1

You need the () after the function name, to call the function.

<select class="selecttype" id="cars">
  <optgroup label="RENAULT">
  <option id="euro" href="mylink" value="euro1">Renault Euro 3</option>
  <option id="euro" href="mylink" value="euro2">Renault Euro 4</option>
</select>

<button class="buttonsea" type="submit" onclick="btnclick()">Search</button>

Then inside the function, you use this, to get the selected:

var cars = document.getElementById('cars')
var selectedCar = cars.options[cars.selectedIndex].value;
Sign up to request clarification or add additional context in comments.

2 Comments

Just adding in this live demo in case it helps.
@Matt Nice! but instead of ".href", then maybe ".value"? Just like I changed mine to :D
0

option element don't have href attribute, but you can send it as data-* attribute and extract it in the JS code. See the example below:

function btnclick() {
  let cars = document.getElementById('cars');
  let selectedCar = cars.options[cars.selectedIndex];
  console.log(selectedCar.getAttribute('data-href'));
}
<select class="selecttype" id="cars">
  <optgroup label="RENAULT">
  <option id="euro" data-href="mylink1" value="euro1">Renault Euro 3</option>
  <option id="euro" data-href="mylink2" value="euro2">Renault Euro 4</option>
</select>

<button class="buttonsea" type="submit" onclick="btnclick()">Search</button>

Comments

0

If i use this code it opens in colsole

function btnclick() {
  let cars = document.getElementById('cars');
  let selectedCar = cars.options[cars.selectedIndex];
  console.log(selectedCar.getAttribute('data-href'));
}

1 Comment

Hey @Bartek. Welcome to the community! I think it'd be best if you use the comments section to continue the conversation on any of the answers. If you intend to open the link instead of writing it to console, you can replace console.log(selectedCar.getAttribute('data-href')); with window.location.href = selectedCar.getAttribute('data-href'); in the snippet you've posted.
0

You can run with calling javascript like this.
I already give a Javascript Result in this code with data-href inside data-* Attribute
Because you can't input href into a Option Selected bcoz Don't have Attribute

See the example below:

function btnclick() {
  let cars = document.getElementById('cars');
  let newSelect = cars.options[cars.selectedIndex];
  let name = newSelect.getAttribute('data-href');
  let result = document.getElementById('result');
  result.innerHTML = "<a href='" + name + "'>" + name + "</a>";
 
}
<select class="selecttype" id="cars">
  <optgroup label="RENAULT">
  <option id="euro" data-href="mylink1" value="euro1">Renault Euro 3</option>
  <option id="euro" data-href="mylink2" value="euro2">Renault Euro 4</option>
</select>
<button class="buttonsea" type="submit" onclick="btnclick()">Search</button>
<br>
<br>
<span id="result"></span>

Comments

0

I made like this and it works :D

function btnclick() {
  let cars = document.getElementById('cars');
  let selectedCar = cars.options[cars.selectedIndex];
  window.open(selectedCar.getAttribute('href'));
}

Comments

0

You can test the solution here https://jsfiddle.net/2pz9se0q/3/

HTML

<select class="selecttype" id="cars">
  <optgroup label="RENAULT">
    <option  data-href="mylink1" value="euro1">
    Renault Euro 3
    </option>
    <option  data-href="mylink2" value="euro2">
    Renault Euro 4
    </option>
  </optgroup>
</select>

<button class="buttonsea" type="submit" onclick="btnclick()">Search</button>

Note: 1- that I deleted the id="euro", you can not give the same id to many tags 2- option group most be closed by </optgroup>

js

function btnclick() {
  let cars = document.getElementById('cars');
  let selectedCar = cars.options[cars.selectedIndex];
  let link = selectedCar.getAttribute('data-href');
  window.open(link, '_blank');
}

Description: when you click the button the function btnclick() will be fired. this function will get the element with id cars. from the element options, it will take the selected option only and save it on selectedCar by using the element selectedCar we can access the data-href attribute and take the link

to open the link on a new window we added '_blank'

Value   Description
_blank  Opens the linked document in a new window or tab
_self   Opens the linked document in the same frame as it was clicked (this is default)
_parent Opens the linked document in the parent frame
_top    Opens the linked document in the full body of the window

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.