SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Sep 2005
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery - getting parameters/values of link?

    Hi,

    I need to fetch 2 parameters/values of a link and save them into
    variables:

    Code:
    <a class="test" href="javascript:myFunction(value1,value2);">Link</a>
    So, how to access either with native JS or with jQuery syntax the
    value1 and value2? Or is it easier to access them through the attribute
    id and name? I know jQuery offeres the command attr but I'm unsure
    how to use it in this way. I want to save the values in a VAR.

    Second problem: I want to pass the 2 saved values to another javascript.
    How is that working?

    thx
    T.C.

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    are you simply trying to get the value from the inline event declaration?


  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    The parameters that you want are within parenthesis, so we can use a regular expression that gets characters that aren't parenthesis with [^()]

    Code javascript:
    regex = /([^()]+),([^()]+)/;
    href = el.getAttribute('href');
    value1 = regex.exec(href)[1];
    value2 = regex.exec(href)[2];

    And here's how you get to the element itself.

    Code javascript:
    var els = document.getElementsByTagName('a');
    var el;
    var i;
    for (i = 0; i < els.length; i += 1) {
    	el = els[i];
    	if (el.className === 'test') {
    		// do stuff here
    	}
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Member
    Join Date
    Sep 2005
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool, thx

  5. #5
    SitePoint Member
    Join Date
    Sep 2005
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is another way to get the values:

    Code:
    function do()
    {
    	var values = document.getElementById('id_von_link').href.replace(/javascript:(.+)\((.+)\);/,'$2').split(',');
    	for(var i = 0;i < value.length;i++)
    	{
    		alert(value[i]);
    	}
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •