简体   繁体   中英

Pass an id to anchor tag

I am trying to pass an id that I get by parsing from the url to an anchor tag. Below is my code which will get the id by parsing it from the url.

Let's take an example, suppose if if id is 123 then my anchor tag should look like this-

<a href="some_url&userId=123"> something here </a>

But somehow with the below code it is not working. Can anyone explain me what wrong I am doing.

<body>
<script>

var id = getUrlParameters()["ID"];    
id = unescape(id);


</script>

// Below things doesn't work with the way I am doing.
<a href="some_url&userId=id"></a>

</body>

I am not able to pass the id to anchor tag successfully. What wrong I am doing here?

Updated Code:-

The actual anchor tag is something like this-

<a href="https://sox.host.net:7020/ps/sox/HRS_HRAM.JN_HRS_CE.GBL&userId=id"></a>

Now I need to replace id withing the anchor tag with 123 basis on whatever the value of id is.

Updated Question:-

I am trying to pass the variable value to an anchor tag. I have a script in Javascript that will get the value from the url. So suppose in the below code,

value of variable id is 54321

Then my anchor tag should look like this-

<a href="some_url&jobId=54321"><img src="url" style="border:none" onmouseover="this.src='url'" onmouseout="this.src='url'"/></a>    

But suppose the value of variable id is 2612 , then my anchor tag should look like this-

<a href="some_url&jobId=2612"><img src="url" style="border:none" onmouseover="this.src='url'" onmouseout="this.src='url'"/></a> 

With the below code, I have, its not working for me. I am not able to pass the id variable value to an anchor tag successfully.

<body>
<script>
function getParameters() {

 // some code

}

var id = getParameters()["ID"];    
id = unescape(id);

// some code

</script>

<a href="some_url&jobId=id"><img src="url" style="border:none" onmouseover="this.src='url'" onmouseout="this.src='url'"/></a>

</body>

You are trying to use & instead of ? which will cause an error.

Try this:

<a href="some_url?userId=123"> something here </a>

Explanation

All i did was change the & to a ? .

Remember these two rules:

  • You should use the ? symbol to connect GET values to a url
  • You should use the & symbol to connect GET values to each other

I hope this helped you out, and feel free to ask for further help!

The parameter you are sending is called userId and the one you try to retrieve is ID . You should use var id = getUrlParameters()["userId"] instead.

EDIT: If your problem is just passing id as an argument to the href , try this:

First, add a tag id to you link tag:

<a href="some_url&userId=id" id="a_tag_id_here">...</a>

Then, add the href with JQuery, using your id variable:

$('a_tag_id_here').attr('href','https://sox.host.net:7020/ps/sox/HRS_HRAM.JN_HRS_CE.GBL&userId='+id);

It's up to you to decide what should trigger this command. If you want it to be set right after the loading of the page, just put this code inside this:

$(document).ready(function() {
  // code here
});

Try this,

var getid= /*your id*/

then get the url from anchor tag using .attr('href') method replace the part after = ,using split('=') method.

forex:

var temp=$('a').attr('href').split('=')[0];
$('a').attr('href',temp+'='+getid);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM