Select next element

Coen Ponsen

I have following code:

<div class="select"><input onclick="return next();" type="radio"></div>
<div class="select" style="display: none" ><input onclick="return next();" type="radio"></div>
<div class="select" style="display: none" ><input onclick="return next();" type="radio"></div>
<script>
function next() {
   $('.select').next().show();
}
</script>

Considering I click the first element's radio button. How can I select the next parent element in line. Right now they all show.

Thanks,

Deepak Kamat

You are using jQuery in the wrong way.

All of the elements has the select class, you have to make it known to the jQuery code that which .select next to which .select you want to show, right now clicking on any .select element triggers the function that shows the next sibling of all of the .select elements.

Instead of using onclick attribute to facilitate the click handler

<script>
function next() {
   $('.select').next().show();
}
</script>

Use jQuery's event attaching

<script>
$(document).ready(function(){
   $('.select').on("click", function(){
       $(this).next().show();
   });
});
</script>

And remove onclick attribute from the elements

<div class="select"><input  type="radio"></div>
<div class="select" style="display: none" ><input  type="radio"></div>
<div class="select" style="display: none" ><input type="radio"></div>

Also note that currently you are looking for clicks on the .select element that contains the radio input, it opens up possibility that the user may not even check the radio input and still the next .select element be shown. That is because the div spans more than the area of the input element. My suggestion is that either limit the width of the .select div element to the width of the inner content i.e the input or attach onchange event to the radio input itself.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I select next 3 siblings of an element which is randomly placed?

How to get select option data in next input element

How to select the next element with same class?

How to select the latest or next element of a list with buttons

jQuery select next child element when class attributes are used

Xamarin.UITest: How to select a next element of the current node

How to select next td tag of selected element by Laravel Dusk or WebDriverBy

How to select next element from sequence in snowflake?

Can we trigger tabkey event for move cursor next element to select2

How to find the next select dropdown nested inside an element?

Select the next span element after an HTML element Jquery

How to display an icon next to a Select element without obscuring the arrow

Select parent element, THEN select next sibling

Select the next DOM element after the current element

Selenium WebDriver - If element present select it, if not ignore it and continue to next element

jQuery select element and next together

How to select next element with unique id on each anchor click?

How can I select the next element using Nokogiri without calling "next_element" method?

How do I select from an array if the next element is not one plus the current element?

How to select an element only if the next element is of a specific type using Nokogiri

Next element in a for

How to select next element to the current

Toggle using only css - select the next element that has a specific class

How to select this element and children of next element in one line?

jquery next div element select

How to move focus to next element on enter on dynamically created select

Python Selenium, select the next element in html DOM

playwright select button by next element

select a next element with a specific class in JS