Using JavaScript and jQuery to Configure an Anchor Tag
April 29, 2014 Leave a comment
We recently helped one of our Exam Engine customers with a custom question template. In this template, they wanted to have a smaller version of an image. The user could then click on the smaller image to see a larger version in a popup window. The first task was to add the anchor tag to the HTML as shown below. The new lines are shown in bold.
<a id=”questionGraphicLink” target=”questionGraphicWindow”>
<img id=”questionGraphic” src=”” alt=”” />
</a>
Next, we used the fact that the larger image would match the name of the smaller image except for a _lg at the end of the name. So if the question image were “xyz.png”, then the large image would be “xyz_lg.png.” We thus added the code below to the function that is called when the page is fully loaded.
// make hyperlink out of questionGraphic if (questionDictionaryId["QuestionGraphic"] != null) { var lgGraphic = questionDictionaryId["QuestionGraphic"].replace(".png", "_lg.png"); $("#questionGraphicLink").attr("href", ("../media/" + lgGraphic)); }
The questionDictionaryId JavaScript objects holds all the items for the question. If it has a “QuestionGraphic” item, we put a reference to the large graphic in the lgGraphic local variable. We then use jQuery to find a reference to the questionGraphicLink anchor object. We then set its href attribute to a relative path to that graphic. If the question does not have a graphic, we skip this step and thus avoid any kind of broken links.