Solved

.next element

Posted on 2014-04-12
1
226 Views
Last Modified: 2014-04-20
I'm trying to figure a way of adding content to the next element after click, so that on click of a button it will find the next matching element and put the results of an ajax response in the matching div, currently Im just going to put some text in.

I thought I could just use:-
$('.menuElemT').click(function () {
    $(this).next('.menuElemL').html('hi-1');
});

Open in new window


So using the html:-
<div class='menuElemT'>Tickets</div>
    <div class='menuElemS'>
        <div class='menuElemL'></div>
        <div class='menuElemR'></div>
    </div>
<div class='menuElemT'>Assetts</div>
    <div class='menuElemS'>
        <div class='menuElemL'></div>
        <div class='menuElemR'></div>
    </div>
<div class='menuElemT'>Reports</div>
    <div class='menuElemS'>
        <div class='menuElemL'></div>
        <div class='menuElemR'></div>
    </div>
</div>

Open in new window


And using the jQuery code:-
$('.menuElemT').click(function () {
    $(this).next('.menuElemL').html('hi-1');
});

Open in new window


By clicking on the div .menuElemT with the content 'Assetts' it should turn it to:-
<div class='menuElemT'>Tickets</div>
    <div class='menuElemS'>
        <div class='menuElemL'>Hi-1</div>
        <div class='menuElemR'></div>
    </div>
<div class='menuElemT'>Assetts</div>
    <div class='menuElemS'>
        <div class='menuElemL'></div>
        <div class='menuElemR'></div>
    </div>
<div class='menuElemT'>Reports</div>
    <div class='menuElemS'>
        <div class='menuElemL'></div>
        <div class='menuElemR'></div>
    </div>
</div>

Open in new window


However when I tie it all together, it doesnt seem to be able to find the element to put 'hi' into:-
http://jsfiddle.net/72DUL/

I dont understand why .next doesnt work.
0
Comment
Question by:tonelm54
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39995964
jQuery.next don't find children, next try to match the NEXT node with the argument no more no less.

Check this : http://jsfiddle.net/72DUL/1/

$('.menuElemT').click(function () {
    $(this).next('.menuElemS').find(".menuElemL").html('hi-1');
});

$('.menuElemL').click(function () {
    $(this).next('.menuElemR').html('hi-2');
});

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question