2

I have structure like this

<div id="mali_oglasi">
    <p class="mali_oglas">
        <span class="name">Predaja7</span>
        <span class="body">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec urna eros, viverra nec porta vitae, luctus at ipsum. Pellentesque porta imperdiet purus, at pellentesque nisl lacinia et. Nam id justo do</span>
        <span class="contact_author">Saša</span>
        <span class="date_created">2012-03-13 14:36:30</span>
        <span class="date_expires">2012-04-12 14:36:30</span>
        <span class="email">[email protected]</span>

        <span>Pirot</span>

        <span>Auto i Moto</span>
                                                                                                   <a class="obrisi" id="16">Obrisi</a>
        <a class="izmeni" id="16">Izmeni</a>
    </p>
    <p class="mali_oglas">
        <span class="name">Predaja6</span>

        <span class="body">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec urna eros, viverra nec porta vitae, luctus at ipsum. Pellentesque porta imperdiet purus, at pellentesque nisl lacinia et. Nam id justo do</span>
        <span class="contact_author">Saša</span>
        <span class="date_created">2012-03-13 14:36:19</span>
        <span class="date_expires">2012-04-12 14:36:19</span>
        <span class="email">[email protected]</span>
        <span>Pirot</span>

        <span>Auto i Moto</span>
                                                                                                   <a class="obrisi" id="15">Obrisi</a>
        <a class="izmeni" id="15">Izmeni</a>
    </p>.....           
</div>

and JS:

$('.izmeni').on('click', function() {
    $(this).closest('.name').replaceWith('<input />');
});

I want to target span with class .name in the parent but nothing is happening???

1
  • 1
    id attributes should not start with numbers. Commented Mar 14, 2012 at 13:08

3 Answers 3

6

You use closest to start beginning at the current element and progressing up through the DOM tree.

Using siblings targets your elements sisters and brother (other DOM elements with the same parent):

$('.izmeni').on('click', function() {
    $(this).siblings('.name').replaceWith('<input />');
}); 

Note: im not sure your using the on() method in the correct manner ... the on() method should be a attached to a parent element of objects that are added after the DOM has loaded, like this :

$(parent).on('click', '.izmeni', function() {
    $(this).siblings('.name').replaceWith('<input />');
});

where parent is a parent of the actual element you are listening on, in your case mali_oglasi could be the parent if the a are to be added within that div

Sign up to request clarification or add additional context in comments.

Comments

0
$('.izmeni').on('click', function(e) {
    $(e.target).closest('.SomeParent').find('.elementToReplace').replaceWith('<input />');
});

1 Comment

Notice the e and closest-find combination.
0

It looks like from your structure .izmeni is a sibling of .name

Take a look at jQuery siblings http://api.jquery.com/siblings/

$('.izmeni').on('click', function() {
    $(this).siblings('.name').replaceWith('<input />');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.