0

I have HTML structure something like this

<div id="DeltaPlaceHolderLeftNavBar">
   <div>
   <div>
      <ul>
         <li><a href="link1.php"><span class="someclass">LINK1</span></a>
            <ul>
               <li><a href="link1a.php"><span class="someclass">LINK1A</span></a></li>
            </ul>
         </li>
         <li><a href="link2.php"><span class="someclass">LINK2</span></a></li>
      </ul>
   </div>
   </div>
</div>

I want to change the HREF of those LI which contain a child UL. As you can see above LINK1 contain UL inside it, but LINK2 doesn't, so for that I wrote the following jquery code but it is not working.

$('#DeltaPlaceHolderLeftNavBar div>div>ul>li>a').click(function()
{
   if($(this).closest("li").children("ul").length > 0)
   {
      //the code comes inside this condition but following line doesn't work
      $(this).closest("li").children("ul").attr("href","#");
   }
});

EDIT

Added "href" tags in code

EDIT Made a mistake in HTML as I forgot one div which I have added now.

Frank Martin
  • 3,147
  • 16
  • 52
  • 73
  • why don't you put the `a` tags in your code as well, so we can understand what is going on.. – Gabriele Petrioli Dec 24 '13 at 10:54
  • 1
    One of the following may work for you: http://stackoverflow.com/questions/179713/how-to-change-the-href-for-a-hyperlink-using-jquery http://stackoverflow.com/questions/4365246/how-to-change-href-of-a-tag-on-button-click-through-javascript http://stackoverflow.com/questions/1334083/how-to-change-href-attribute-of-a-link-using-jquery – Kamran Ahmed Dec 24 '13 at 10:54
  • 1
    you need to change the `children('ul').attr('href','#')` to `children('a').attr('href','#')` – Pete Dec 24 '13 at 10:54
  • 1
    also... `ul` elements do not have a `href` attribute.. – Gabriele Petrioli Dec 24 '13 at 10:56
  • 1
    You are asking the same question again and you even marked the other one as answered.... http://stackoverflow.com/questions/20757519/how-to-find-if-li-has-children-ul/20757579#20757579 – Milanzor Dec 24 '13 at 10:57
  • @Milanzor This isn't the same question. He's using that solution, and asking a question about the new code. – Barmar Dec 24 '13 at 11:03
  • @Barmar Hmm, true, I guess it is a different question, but I won't be suprised when he opens a new question after this one is answered. – Milanzor Dec 24 '13 at 11:09
  • @Milanzor Me either. He doesn't know how to program, so he's getting us to write his code for him by asking a question about each line. – Barmar Dec 24 '13 at 11:10
  • I know programming but not jquery. I know a little bit of it but not fully. – Frank Martin Dec 24 '13 at 11:14

1 Answers1

2

I think you just need to change your code

$(this).closest("li").children("ul").attr("href","#");

with

$(this).closest("li").children("a").attr("href","#");

in your if condition.
Now your click event function become :

$('#DeltaPlaceHolderLeftNavBar div>div>ul>li>a').click(function(event)
{
    event.preventDefault();

    if($(this).closest("li").children("ul").length > 0)
    {
        //the code comes inside this condition but following line doesn't work
        $(this).closest("li").children("a").attr("href","#");
    }  
});  

Here i have add event as function argument and event.preventDefault() to prevent default your link click event to not redirect on the link address.

Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50