views:

29

answers:

2

I have a parent div tag like this

<div class="top-news-heading">Top News Items<br>
<ul>
   <li>
      <div id="xlaANMzone_4f503f">
       <div id="4f503f">
          <div align="left" style="width: 100%; padding: 0px; margin: 0px;">
              <a href="http://www.onesite.com.au/news_manager/templates/?a=1231&amp;z=44"&gt; News 1</a><br>
              <a href="http://www.onesite.com.au/news_manager/templates/?a=1232&amp;z=44"&gt; News 2</a><br>
              <a href="http://www.onesite.com.au/news_manager/templates/?a=1233&amp;z=44"&gt; News 2</a><br>

        </div>
      </div>
     </div>
   </li>
   <li>
      <div id="xlaANMzone_4f503f">
       <div id="4f503f">
          <div align="left" style="width: 100%; padding: 0px; margin: 0px;">
              <a href="http://www.onesite.com.au/news_manager/templates/?a=1236&amp;z=44"&gt; News 4</a><br>
              <a href="http://www.onesite.com.au/news_manager/templates/?a=1238&amp;z=44"&gt; News 5</a><br>
              <a href="http://www.onesite.com.au/news_manager/templates/?a=1230&amp;z=44"&gt; News 6</a><br>

        </div>
      </div>
     </div>
   </li>
</ul>

</div> <!--parent div ends-->

I want to change href of every child tag to http://www.offsite.com/article.asp/a=1238&amp;z=44. using Jquery or javascript....

Note that query strings "a" and "z" should be the original ones. any help will be appreciated.

+2  A: 
 $(document).ready(function(){
      $(".top-news-heading a").attr("href","your_new_link");
    });
Marko
@@Marko... thanks for reply. but i need only to replace http://www.onesite.com.au/news_manager/templates part only..than How to do.?
Anil
yes, but I think it is more elegant to first construct your whole new link, and then just set your hrefs, instead of doing replacing with jquery which could be error-prone
Marko
Marko but Qurystrings "a" and "z" are dynamically coming from backend.. i cant predict these two. so what next ?
Anil
yes, but you can construct that link from a and z coming from backend, in your javascript code for example, right? tomorrow you'll be needed some other link and then ask your self, is it better to just change construct of that link or to change jquery replace to meet your new needs...try to look your link as a variable not a const...cheers
Marko
anyway, thats just my concern, you can go with jquery replace if you prefer that style, my obligation was to warn you ;)
Marko
@Marko thanks, But how can i attach dynamic values of "a" and "z" in the new href vlaue... can you please help.
Anil
you have your defined link without "a" and "z" values in your code behind...then extract those values and add them to your link...and final, use your link to create javascript variable...if you send me language that you use in backend, I could help you better...in php it would be something like this: <script type="text/javascript">var link = <?php echo $your_constructed_link ?></script>
Marko
@Marko.. thanks Buddy... I corrected this from backend. You helped me.
Anil
no problem, glad that I helped :)
Marko
+1  A: 

If you just want to replace the string like I guess do

$('a').each(function(){
    var $this = $(this);
    $this.attr('href', $this.attr('href').replace(/onesite/, 'offsite'));
});
jAndy
@@jAndy thanks for reply..I need to replace only under parent div.top-news-heading only..
Anil