-1

On my page there are a lots of links. Some of these links has fileType querystring. Lİke this :

<a href="file.php?some=query&fileType=...">LINK</a>

But fileType querystring's value is dynamic . I'm there is a radio button like this . User choosing file type with this, XML or XLS . If user choose XML , links will be like this

<a href="file.php?some=query&fileType=xml">LINK</a>

if user choose XLS links will be like this

 <a href="file.php?some=query&fileType=xls">LINK</a>

fileType querystring's value is coming with type JS variable. Now how can i add this variables value to my links. I tried this but it's not working.

<a href="file.php?fileType<script>return type;</script>">LINK</a>
Eray
  • 7,038
  • 16
  • 70
  • 120

1 Answers1

1

If you give the anchor tag an id, exampleID, you can set the href attribute in js

document.getElementById('exampleID').href = 'file.php?some=query&fileType=' + type;

jQuery similar:

$('#exampleID').attr('href', 'file.php?some=query&fileType=' + type);

and using jQuery class selector:

$('.myClass').attr('href', 'file.php?some=query&fileType=' + type);
Nick Rolando
  • 25,879
  • 13
  • 79
  • 119
  • If i want to do this with class (because there are a lot's of link) what should i use instead of getElementById ? – Eray Dec 20 '11 at 22:25
  • BUt other querystrings (i.e. `some` querystring) are changing for every link. Because of this i have to do this inline. I mean something like this : `LINK` – Eray Dec 20 '11 at 22:29
  • @Eray: Then just *append* to the URL, or use some function to parse the parameters and put the URL together again. You can also use classes to identify the links, there are many ways to retrieve a reference to a DOM element. – Felix Kling Dec 20 '11 at 22:34
  • @Eray Are you able to change all the href attributes at the same time, say towards the end of your html document? You can do them all like this. – Nick Rolando Dec 20 '11 at 22:35
  • @FelixKling , how can i "append" ? – Eray Dec 20 '11 at 22:37
  • 1
    @Eray: Retrieve the current value, append to the value using string concatenation, set the new value. Or simply `a.href += 'foo';`. Just basic string concatenation and variable assignment. – Felix Kling Dec 20 '11 at 22:40