11

I am trying to create a series of resource boxes that will be displayed on page as an image and title.

images with captions

When an image (resource) is selected (clicked on) a dropdown box containing a brief description of the resource will be displayed, and a button for more info.

more info option appears

When the resource is clicked on again it will display the full description of the resource, and two buttons: one to remove the info and one for less info.

edit info on media

I have used XSLT to select the content for each resource based upon the XML provided when a user inputs a resource.

XSLT:

<div id="newContainer">
<div class="hidden">
<xsl:value-of select="/Properties/Data/Group[@Name='Header']/Datum[@Name='Title']"/>
</div>
    <ul id="initialContent">
        <xsl:for-each select="/Properties/Data/Group[@Name='List Item']">
            <li>
                <xsl:attribute name="class">
                    <xsl:value-of select="Datum[@Name='Title']"/>
                </xsl:attribute>
                <a href="#">                
                        <img>
                            <xsl:attribute name="src">
                                <xsl:value-of select="Datum[@Name='Source']"/>
                            </xsl:attribute>
                            <xsl:attribute name="alt">
                                <xsl:value-of select="Datum[@Name='Alt']"/>
                              </xsl:attribute>                      
                        </img>  
            <xsl:value-of select="Datum[@Name='Title']"/>   
                </a>
            </li>
        </xsl:for-each>
    </ul>
</div>
<div id="briefDescription">
<xsl:if test="@class=Datum[@Name='Title']">
     <xsl:for-each select="/properties/Data/Group[@Name='List Item']">
        <a>         
        <xsl:value-of select="Datum[@Name='Description']"/>
        <xsl:value-of select="normalize-space(substring(title,1,50))" disable-output-escaping="yes"/>
    <xsl:if test="string-length(title) &gt; 50">..</xsl:if>         
        </a>
    </xsl:for-each>
</xsl:if>
</div>

XML:

<Data>
<Datum Exposed="true" ID="ResourceTitle" Name="Title" Type="String">Title</Datum>
<Group Exposed="true" ID="Resource" Name="List Item" Replicatable="true">
<Datum Exposed="true" ID="ResourceName" Name="Name" Type="String">Resource Name</Datum>
<Datum Exposed="true" ID="ResourceSource" Name="Source" Type="File"><![CDATA[$URL_PREFIX/assets/sites/hea1/pages/hea-building.jpg]]></Datum>
<Datum Exposed="true" ID="ResourceLink" Name="Link" Type="File"><![CDATA[Http://www.bbc.co.uk]]></Datum>
<Datum Exposed="true" ID="ResourceAlt" Name="Alt" Type="String">Alt Text</Datum>
<Datum Exposed="true" ID="ResourceDescription" Name="Description" Type="Textarea">Introductory Text</Datum>
</Group>
</Data>

I have then used jQuery to get the boxes to slide down, slide up, hide etc.

I am having trouble ensuring that the XSLT formats the correct resource information, when a resource is selected.

Can I use jQuery to identify a class and then use XSLT to check that class against an XML node? For example:

function getClassName() {

$('li').click(function(){
var chosenClass = $(this).attr('class');
alert(chosenClass);
});

}

<xsl:when test ="$chosenClass and Datum[@Name='Title']">

I have given the selected resources a unique ID and used the XSLT to set a var class name that is provided by the XML node name Name.

The following jQuery is what I have so far. It does what it should until I try and set the class at the id $('#briefDescription').

$(document).ready(function () {
getClassName();
displayContent();
infoMsgRemove();
removeInfo();
moreInfo();
});

var briefInfo = $('#briefDescription');
var moreInfo = $('#moreInfo');
var fullInfo = $('#fullDescription');
var newBriefInfo = newBriefInfo;

function getClassName() {

$('a').click(function () {
    var chosenClass = $(this).attr('class');
    var selectedClass = chosenClass;
    alert(selectedClass + ' chosen');

    briefInfo.addClass(chosenClass);
    var newBriefInfo = briefInfo.addClass(chosenClass);
    var createdBriefInfo = newBriefInfo;
    alert('class added'+ briefInfo.text());
});
}

function displayContent() {

$('#initialContent').click(function (e) {  

    if (newBriefInfo.is(':hidden')) {
        newBriefInfo.slideDown('fast');
        moreInfo.show('li');
    } else {
        fullInfo.slideDown('fast');
        moreInfo.hide('li');
    }
    e.preventDefault();
});
}

function moreInfo() {

$('#moreInfo').click(function () {
    if (fullInfo.is(':hidden')) {
        fullInfo.slideDown('fast');
        moreInfo.hide('li');
    }
});
}

function infoMsgRemove() {

$('#noInfo').click(function () {
    fullInfo.hide();
    briefInfo.hide();
    alert('msg hidden');
});
}

function removeInfo() {

$('#lessInfo').click(function () {
    fullInfo.slideUp('fast');
    moreInfo.show('li');
});
}

I have created a JSFiddle that hopefully goes someway to showing what i am trying to do.

In the function getClassName the alert;

alert('class added'+ briefInfo);

retuns 'class added [object object]'.

I think this is because the HTML is being populated by a string representation of an object?

Here is the work in progress fiddle.

11
  • 1) What you mean exactly under "drop down"? The HTML you provided contains no comboboxes (dropdowns). 2) "Each resource has its own xml content" - do I get it right, that actually you mean "XSLT script iterates over XML-elements and generates HTML-element (image display and text description) for each XML-element"?
    – akhikhl
    Commented Jan 4, 2014 at 20:07
  • 3) "to get the drop down box to only display the content for the selected (clicked on resource)." - do I understand it right, that there should be MANY HTML-elements associated with resources and ONLY ONE HTML-element for the drop-down?
    – akhikhl
    Commented Jan 4, 2014 at 20:13
  • How do you define "selection"? Do you consider HTML-element "selected" when [it is focused or contains focused element]?
    – akhikhl
    Commented Jan 4, 2014 at 20:16
  • you seem to be a livesite developer. if you clearly explain what you i can help you..you mean when you click on image you show up some text ? Commented Jan 5, 2014 at 7:32
  • I dont see you using $(this) anywhere in your code ??? and if you can clearly say the order of the divs then using $(this).next() you task can be done. and having h1 tag inside a tag is not allowed Commented Jan 5, 2014 at 7:44

3 Answers 3

1

The easiest way I think would be to give the 'buttons' a shared class and a custom id. Then use jQuery to read the custom id of the clicked buttonclass-item and use a naming policy on the class of the description to make that display correctly.

Html:

<a href="#" class="myResources" id="resource_1">
    <h2>resource 1</h2>
</a>

And Javascript/jQuery:

$(".myResources").click(function() {
//And use the same id as class of the description for easy targetting.
var resName = $(this).attr('id');
    $('.'+resName).show();
});

Hope this is something like what you we're looking for. Might not be the cleanest solution. HTML5 custom data-attributes might be a better fit.

0

If you want to check whether your element has a class or not, you can use jQuery's hasClass() function:

$('a').click(function () {
if( $(this).hasClass('yourClass') ) { 
  //Do whatever this class needs to do
else {
 // Do other stuff...

More information on the hasClass function can be found in the JQuery API Documentation

0

Had your tried to alert("class added "+$(briefInfo).attr("class"));

if you got the right alert then you can use your logic in this way by using the class.

Not the answer you're looking for? Browse other questions tagged or ask your own question.