deadlyp99 Posted December 14, 2012 Share Posted December 14, 2012 (edited) Hello, i've been working on a site, and there is a specific element I wish to edit. The trouble I have is there are many instances of the class, so I've been using .parent() in jquery, and I feel I am just doing it wrong. The markup looks like this <div class="disignBoxFirst bx-def-margin-top bx-def-border"> <div class="boxFirstHeader bx-def-bh-margin"> <div class="dbTitle">Ad_Rotator</div> <div class="clear_both"></div> </div> <div class="boxContent"> <div class="dbContentHtml"> <p id="motivationals"></p> <script type="text/javascript">// <![CDATA[ /**buildAdRotator();**/ // ]]></script> </div> </div> </div> I am trying to edit the innerHTML of dbTitle. The div is the first instance of the class on the site, but I don't want code to break if I place new ones before it in the future. So I wish to use the motivationals id as a handle or reference point since there will only be one instance of that. I've tried $('#motivationals').parent().parent().parent().first('.dbTitle').html('aasdf'); This doesn't behave as I want it to, and I really feel i'm using bad code just looking at that. Due to the nature of the script I am working on, editing the markup isn't really an option outside of the dbContentHtml div. Any insight on how to properly hop up to the parent, and then down to dbTitle would be much appreciated. Edited December 14, 2012 by deadlyp99 Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 14, 2012 Share Posted December 14, 2012 I don't know the answer to your question, but is there a reason you can't add an id to that field and make this simple? Quote Link to comment Share on other sites More sharing options...
deadlyp99 Posted December 14, 2012 Author Share Posted December 14, 2012 The script I'm working with allows easy content to be added. Throw it a title and a body of content and it throws a div on the page. I'd really rather avoid editing the base script for this, because I don't know how it may effect the rest of the site. Quote Link to comment Share on other sites More sharing options...
codefossa Posted December 14, 2012 Share Posted December 14, 2012 Is there a reason you can't put the ID on the title instead? .find(".dbTitle:first") or .find(".dbTitle").first() You're using the $.first() function incorrectly. Quote Link to comment Share on other sites More sharing options...
deadlyp99 Posted December 14, 2012 Author Share Posted December 14, 2012 (edited) The dbTitle class holds the value of the div's title in the database. That title is used to manage the div's content in the CMS. I could have bypassed all of this by not naming it. But then in the future I could have many divs with no names, which makes determining what's what messy. But any divs created through the cms get that class to conform to the site's template. Adding an id to it would require me to recode the cms to allow me to add a unique id. Ideally that is what I would want right, but I'm not familiar enough with the very large script to properly do it in a timely manner... It requires restructuring the database, updating all the divs that don't have id's, updating all the backend php for add, editing, and removing said content, as well as possibly needing to modify code that needs to interact with the structure or content of the div's. So I can do that, or throw a simple edit and put the long and hard on my to-do list. I had been throwing various functions at jQuery because I don't know it 100% yet. Thanks for helping out, I have gotten this working, and I got the parent function working ... more cleanly. It appears this will do what I want it to and I don't foresee problems with it so far. $('#motivationals').parents('.disignBoxFirst').find('.dbTitle:first').html(''); Thanks a ton. Edited December 14, 2012 by deadlyp99 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.