Jump to content

Get element by ID to complete a URL string


dhcrusoe

Recommended Posts

All,

 

I'm working with a bit of pesky ASP code, and have a challenge I'd love some help with. The challenge is this: the ASP outputs one tag that I need to use twice. Here's a code example of what I have:

 

<a id=newestuser href="http://www.test.com/user?name=tom"></a>

 

and here's a code example of what I need:

 

<a id=newestuser href="http://www.test.com/user?name=tom">tom</a>

 

Either that, OR the javascript could simple read in the variable (tom) and output it twice - once into the href tag, and once into the link/title area of the <a> tag.

 

Any thoughts/suggestions/ideas? (I'm a beginner JavaScripter -- sorry if it's a basic question, and thanks for your help!)

 

--Dave

Link to comment
Share on other sites

Hey there --

 

I'm editing an installed version of DotNetNuke, and the modules that output the ASP are already compiled. So, there isn't much I can do to access everything, unless I were able to get into the source & recompile the module. So, in this case, I really (really) prefer to go the non-ideal JS route (even though, yes, it's FAR better to go with ASP).

 

The basic problem I'm having is that, in the code, it won't let me repeat a tag twice, for instance:

 

<asp:Label id="lblAuthor" Runat="server" cssclass="blog_Description"></asp:Label></TD>

 

I'd far prefer that method, but nothing I've tried has worked..

 

Thanks!

--Dave

Link to comment
Share on other sites

Is the id for the anchor tag known to you before hand? i.e., can your js script know what id to look for?

If so, a document.getElementById() and some slicing could extract the value of name= and insert it into the innerHTML.

 

If you don't know id before hand, then you can use a getElementsByTagName type deal, and check them all to see if they have anything for their innerHTML. If they don't, then do the slicing/inserting.

 

Hopefully that makes sense.

Link to comment
Share on other sites

off the top of my head and untested:

 

window.onload = function ( ) {
  var eles = document.getElementsByTagName('A');
  var matches;
  for(var n=0;n < eles.length;n++){
    if(eles[n].innerHTML == '' && matches = eles[n].href.match(/user?name=(.+)$/))
      eles[n].innerHTML = matches[1];
  }
}

Link to comment
Share on other sites

Hey all --

 

In the end, here's how it was resolved:

<script type="text/javascript">
function createLink() { 
var link = document.getElementById('userlink').innerHTML;
var newLink = 'http://www.test.com?user=' +link;
window.location = newLink;}
</script>

<a href="javascript:createLink();" id="userlink">test</a>

 

Thanks for all your help!

--Dave

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.