Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass Dynamic URL in Javascript with attributes

Status
Not open for further replies.

monkey64

Technical User
Apr 27, 2008
69
GB
Hi there.

I'm using the excellent lightbox script to show some images. The recommended syntax is:

<a href=" rel="lightbox">

I have the code working with a static image, but I have a select box with multiple values, and I want the URL above to be based on a user's selection., but I also need to somehow add the "rel" tag.

The code below works fine, but the Lightbox script won't run as the "rel" tag is missing. If I do this:

<a href="javascript:a(1)" rel="lightbox">

the script does not access the image and stalls. I don't know what I need to do. Any ideas?

Code:
<script type="text/javascript">

var acc040rb = new Image().src = "images/basket/acc040-rb.jpg";
var acc040bo = new Image().src = "images/basket/acc040-bo.jpg";
var acc040by = new Image().src = "images/basket/acc040_by.jpg";

function setImage(imageSelect, myid) {
 theImageIndex = imageSelect.options[imageSelect.selectedIndex].value;
 if (document.images)
     document.getElementById(myid).src = eval(theImageIndex);
  }

function a(myid) {
location.href=document.getElementById(myid).src;
}

</head>
<body>

<div class="outer">

<select name='items1' class="select" onChange="setImage(this, 1)">
	<option value="acc040rb">Red &amp; Blue</option>
	<option value="acc040bo">Blue &amp; Orange</option>
	<option value="acc040by">Blue &amp; Yellow</option>
</select>

<a href="javascript:a(1)"><img src='images/basket/as10sh.jpg' border="0" class="multiple" id="1" ></a>
		<div class='desc_multiple desc'>My product description<span class='price'>&pound;14.50</span></div>
		<div class='button'><a href="basket.php?Id=ASL203-BK&amp;Description=My Products&amp;Price=&amp;Vat=14.5&amp;PostageCode=0&amp;UQty=1&Unit=Each">
        <img src="images/btn_accs.gif"  border="0" class="button_desc"></a></div>
</div>
 
Hi

Why not simply change the [tt]href[/tt] ?

As far as I know the light* things, the [tt]a[/tt] element will work only if JavaScript is disabled, otherwise the script will take the [tt]href[/tt] and display the referred image itself. So putting [tt]javascript:[/tt] calls in the [tt]href[/tt] I would never expect to work. However, I may be wrong.

By the way, "1" is not a valid [tt]id[/tt]. See Element identifiers: the id and class attributes and SGML basic types :
Element identifiers: the id and class attributes said:
[tt]id[/tt] = name [CS]
This attribute assigns a name to an element. This name must be unique in a document.
SGML basic types said:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top