Search code examples
jqueryhtmlcssposition

Using jQuery to position one element next to another no matter the value


this is a wonderful website. I am a new programmer who is having trouble trying to figure out how to position one element next to another no matter what the prior element is.

For Example, If I have element A that can have a value of anything And if I have element B which looks like the word "Dog".

I want to place Dog right after element A.

So if element A is Cat: I want it to show "Cat Dog" And if element A is Hippopotamus: I want it to show "Hippopotamus Dog"

The reason I say this is because I am having spacing issues. It is my understanding this can be accomplished with jQuery.

Here is an example css:

Element A {
    position: relative;
    font-size: 15px;
}
Element B {
    position: relative;
    font-size: 15px;
}

Thank you for reading. Note: Neither element is a child of another or anything like that. They are completely random elements. Also the code has to be sensitive to browser resizing.


Solution

  • The jQuery .after() method will move an element after another element.

    $("Element A").after($("Element B"));
    

    Browser resizing is irrelevant. If an element is after another element, it will be there regardless of the size.