jquery: how to work with elements in area of specified html element?

jquery get html element
jquery html
jquery create element
jquery find
jquery add text to div
jquery append multiple elements
jquery create html element
jquery get innerhtml

How can I set img.src only for specified the img? select and target img must have the same parent div.

$(".selector").change(function() {
  $(".target_image").attr("src", $(".selector").val())
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <select class="selector">
    <option value='src1'>1</option>
    <option value='src2'>2</option>
  </select>
  <img class='target_image' src='' \>
</div>
<div>
  <select class="selector">
    <option value='src3'>1</option>
    <option value='src4'>2</option>
  </select>
  <img class='target_image' src='' \>
</div>

You can use DOM traversal to find the target element starting from the element which invoked the event. For example:

$(".selector").change(function(){
    $(this).closest("div").find(".target_image").attr("src", $(this).val());
});

This starts at this, which is the instance of .selector that raised the change event, navigates to the closest parent div, then searches that div for matching .target_image elements (of which there is one). Then it sets the src attribute on that element to the value of the .selector which raised the event.

Note that any jQuery DOM navigation, whether traversing up and then back down or whether traversing across sibling elements, is subject to breaking if the DOM structure ever changes. It's up to you to decide which changes are more likely or less likely than others.

.html(), Get the HTML contents of the first element in the set of matched elements or set the HTML This can occur by injection of script tags or use of HTML attributes that Given a document with six paragraphs, this example will set the HTML of <​div  In order to get the best performance using :selected, first select elements with a standard jQuery selector, then use .filter( ":selected" ), or precede the pseudo-selector with a tag name or some other selector. link Selecting by type. jQuery provides pseudo selectors to select form-specific elements according to their type::password:reset:radio:text


Use siblings function to select the image that is a sibling of the currently selected element, use $(this).val() to get the value of the current selected element

$(".selector").change(function(){
    $(this).siblings(".target_image").attr("src",$(this).val())
});

jQuery(html, attributes), A DOM Element, Document, or jQuery to use as context elementArray. Type: Array. An array containing a set of DOM elements to wrap in a jQuery object. With jQuery you can add and remove HTML elements on a page, and modify their content. Adding New Elements There are two ways to add new elements in a web page, you can create the element and then insert it in the page to a specific location, or you can directly add the HTML item in the page.


.contents(), Description: Get the children of each element in the set of matched elements, Given a jQuery object that represents a set of DOM elements, the .contents() method holds a numeric code indicating the node's type; text nodes use the code 3. When we select an element with jQuery, a jQuery object is returned with a collection of elements that match the specified selector. Therefore, to get the reference to the original HTML element we either use the get () method or the array notation [] when using hasAttribute with jQuery


In your case the following code will work. Because in your case image is the next element of the .selector so it can be traverse by next() method. You can read more about jQuery's next() method here.

$(".selector").change(function(){
    $(this).next("#target_image").attr("src",$(this).val())
});

or:

$(".selector").change(function(){
    $(this).parent().find("#target_image").attr("src",$(this).val())
});

jQuery parent() method selects the parent of the element and only travels a single level up the DOM tree, and then we can find the image inside it.

.append(), DOM element, text node, array of elements and text nodes, HTML string, or jQuery The .append() method inserts the specified content as the last child of each element in the jQuery collection (To insert it as the first child, use .prepend() ). The jQuery prepend () method inserts content AT THE BEGINNING of the selected HTML elements. $ ("p").prepend("Some prepended text."); Try it Yourself » Add Several New Elements With append () and prepend () In both examples above, we have only inserted some text/HTML at the beginning/end of the selected HTML elements.


.wrap(), A selector, element, HTML string, or jQuery object specifying the structure to wrap around the matched elements. When you pass a jQuery collection containing  With jQuery, it is easy to remove existing HTML elements. The jQuery remove () method removes the selected element (s) and its child elements. The jQuery empty () method removes the child elements of the selected element (s). The jQuery remove () method also accepts one parameter, which allows you to filter the elements to be removed.


.before(), Description: Insert content, specified by the parameter, before each element in the set of HTML string, DOM element, text node, array of elements and text nodes, This can occur by injection of script tags or use of HTML attributes that  jQuery Selectors. jQuery selectors allow you to select and manipulate HTML element(s). jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors.


.after(), content, specified by the parameter, after each element in the set of matched elements. HTML string, DOM element, text node, array of elements and text nodes, DOM element(s), text node(s), or jQuery object to insert after each element in the set This can occur by injection of script tags or use of HTML attributes that  In the above jQuery code, there is a click event attached to a button which appends a div element to HTML body. And there is also a click event attached to all div elements. In this case, the click event will work properly for the div element present on the page while attaching and will not work for dynamically added div elements.