tags:

views:

182

answers:

5

Hi, I am facing one small issue. I want to change the parent div style as the content of the div increase its text chars.

<div class="welcome-message"><a href="#">Some link</a></div>

this is the format of the html. lets say it the link text has min char 80 chars, if its more than 80 then the style of the parent div should be change ie. width:200px;

jQuery solution preferred , JS also fine.

thanks

A: 

There's no javascript event such as "did the content inscrease ?" dynamic style, all you can do is ask in javascript "did the content increase" every n ms.

But i think your problem is much more a css one ...

vvo
cant we find the char length using jQuery, and some If else condition or some thing like that
Wazdesign
Yes but all solutions here are on the document ready, if the content of the link change then you'll not know it because there's no js events that says "content updated" ...
vvo
thanks Vincent, its actually a username. welcome username. so its document ready. and its not change per ms. thanks for answering Vin,
Wazdesign
I also think this is an Issue which can be solved by CSS. I had a similar problem on a Project which had to auto-enlarge the parent div, while keeping it centered. I solved it with CSS but don't recall how exactly I did it (a lot of trial and Error was involved)
Mike
+2  A: 

You can do it like this:

$(document).ready(function()
{
    if ($('.welcome-message a:first').text().length > 80)
        $('.welcome-message').css({'width': 200});
});
Greg
triggered only once, i dunno if it's the behavior he's excepting
vvo
this really nice solution just have to add 'width':200+'px' rest is fine.. thanks Greg
Wazdesign
Hmm jQuery should automatically add the "px" for you
Greg
works when you do .width(); not .css
vvo
If you do .css('width', 200) it automatically adds "px"... docs don't say one way or the other for .css({})
Greg
+1  A: 
$(document).ready(function(){
    var l=$("div.welcome-message").text().length;
    var w=50;

    if(l>80){
        w=200;
    }else if(l>40){
        w=100px
    }
    $("div.welcome-message").css({'width':w+'px'});
});
TheVillageIdiot
+1  A: 

Well, assuming you have a separate class that defines the new, larger width:

function checkLength()
{
   if ($('div.welcome-message a').text().length > 80)
   {
      $('div.welcome-message').addClass('myLargeWidthClass');
   }
   else
   {
      $('div.welcome-message').removeClass();
   }
}

You would then call this function on load. E.g.

$(function(){
   checkLength();
});
Phil.Wheeler
+1  A: 

I know this doesn't answer your question because your div will only change on initial page load, but after reading through the answers for this question I wanted to add this solution for completeness as there is an event that triggers when there is ANY change to the element in the DOM (except for IE)... I found the solution here on SO.

In summary, this works to resize the <div> based on it's dynamic content (note the "DOMSubtreeModified")

 $(".welcome-message a").bind("DOMSubtreeModified",function(){
  var w = ($(this).html().length > 80) ? 200 : 100;
  $(this).parent().css("width", w);
 })

I also added a sample of the code in action to this pastebin.

fudgey
thanks for the Solution. and pastebin
Wazdesign