Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Changing styling of parent element without unique id or class

I have a lot of divs identical to the one below

<div class="example">

Some of these divs have child nodes, which look something like this

<input type="submit" name="auto-gen" value="Extra" id="auto-gen" class="add-button" aria-label="" data-active="True" data-isfirstcontrol="False" type="button">

Due to working with legacy software, I can’t change anything about the structure of these divs (can’t change id’s or change class name or anything). However, I want to be able to add some CSS (float: right;) to the example class, but only if they have a child with the "add-button" class.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve been trying to do stuff like below:

$('.add-button').parent().style.styleFloat = 'right'

However, whatever I try I get the following error message

Uncaught TypeError: Cannot set properties of undefined (setting 'styleFloat').

Anyone have any ideas?

>Solution :

If there are no .add-button elements which parent() is not a .example, you can just target every add-button and style his parent()


Using

$(".add-button").parent().css({ float: "right" });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="example">
  
</div>

<div class="example">
    <input type="submit" name="auto-gen" value="Extra" id="auto-gen" class="add-button" aria-label="" data-active="True" data-isfirstcontrol="False" type="button">
</div>

Using plain
You might not need Jquery

document.querySelectorAll('.add-button').forEach(e => e.parentNode.style.float = 'right')
<div class="example">
  
</div>

<div class="example">
    <input type="submit" name="auto-gen" value="Extra" id="auto-gen" class="add-button" aria-label="" data-active="True" data-isfirstcontrol="False" type="button">
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading