Skip to content
Advertisement

How can I choose the option in the selectbox according to the data that comes with the get method?

JavaScript

According to the getBt variable here, how can I do ‘checked’ from the options below to the value equal to value?

So, for example, if the value of getBt is 2, I want to select the radio option with value 2.

JavaScript

Advertisement

Answer

Let’s assume your method of parsing URL is working fine, and variable getBt contains the value that you wanted to be checked in radio button, there are couple of solution that you might try.

1. Using Vanilla JS

We can use attribute selector in DOM by using document.querySelector() and document.querySelectorAll() methods.

JavaScript

2. Using jQuery

JQuery makes DOM manipulation easier, and for including that in your project, include the following in your project html file:

JavaScript

Now in JS file, just write this:

JavaScript

PS: You can choose any solution to solve the issue. Hope it works.

You can find both version of solution in this Stackblitz link

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement