0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="js/jquery-1.10.2.min.js" ></script>

<title>Untitled Document</title>
<script type="text/javascript">    
$(document).ready(function(){
    $('#show').click(function(){
    var href =  $(this).attr('href');    
    alert(href);
    });
    }); 
</script>
<style>
.show{
    background:#693;
    color:#000;
    text-decoration:none;
}
</style>            
</head>        
<body>
<p class="credits">Figure credits goes here</p>
</figure>    
<p>&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test</p>    
<p>&nbsp;</p>    
<figure class="image"><img alt="" height="228" src="http://localhost/rvrite/uploader/img/541akki.png" width="300" />
<figcaption>
<p><a class="figure-lable" id="f2" name="figure2">Figure 2&nbsp;</a></p>    
<p>&nbsp;</p>    
<p>Caption</p>
</figcaption>    
<p class="credits">Figure credits goes here</p>
</figure>    
<p>&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo <a href="#table2(t2)" id="ytD2F">table2</a>&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;</p>    
<table border="1" cellpadding="1" cellspacing="1" style="width: 500px;">
    <caption><a class="figure-lable" id="t1" name="table1">Table 1&nbsp;</a>caption goes here</caption>
    <tbody>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            </tr>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </tbody>
</table>    
<p>demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;</p>    
<table border="1" cellpadding="1" cellspacing="1" style="width: 500px;">
    <caption><a class="figure-lable" id="t2" name="table2">Table 2&nbsp;</a>caption goes here</caption>
    <tbody>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </tbody>
</table>
<p>&nbsp;</p>
<input type="button" value="ok" id="show" />
</body>
</html>

What I want to do is to replace all my href string href="#table2(t2)" with the string present inside the braces :(t2)

if it is : href="#table3(t3) it should be replace with t3 

how can I do it using jquery?

2
  • possible duplicate of How to change the href for a hyperlink using jQuery Commented Apr 6, 2015 at 8:33
  • Changing hrefs in this way is pretty unusual and probably unnecessary. I'm pretty sure there's a better solution to whatever you are trying to do. Commented Apr 6, 2015 at 10:55

2 Answers 2

2
var href1=document.getElementsByTagName('a');
    for(i=0;i<href1.length;i++) {
       var val=href1[i].getAttribute('href');
        if(val!=null) {
           var str=val.substring(val.lastIndexOf('(')+1,val.lastIndexOf(')'));
           href[i].setAttribute('href',str);
        }
    }

Check Here

Sign up to request clarification or add additional context in comments.

10 Comments

Check the demo.. @imrahul
srry friend i hope u did not get my point what i want to do get the value of href and then replace it with value present in the braces of that particular string
i need to replace the val with str in href onclick event ..how would i replace
Check now .. you need to add $(this).attr("href",str); in place of alert(str) @imrahul
suppose if i want to apply this effect on each and every href tag of a page how can i do it and thankx for helping me till this time
|
0
var regExp = /\(([^)]+)\)/;
var val="table3(t3)";
var matches = regExp.exec(val);
alert(matches[1])

check here

2 Comments

What if the string is like "tab(le3(t3)" ?? @AmitSoni
In that case, it will not work as we have defined a pattern.. You have to make sure for ID generation. Moreover these kind of scenarios should be handle with regular expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.