Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Child Control in Java Script ?


Reply

6 Posts   637 Views

Avatar
Noki

10 June 2012 at 2:18am Community Member, 6 Posts

Hey,
I try to fill an array for a JS Timeline with Data from Silverstripe.

I'm looking for something like:

<script type="text/javascript">
...
var event_data =
{
"dateTimeFormat": "ISO 8601",
"events":[
         
<% control children %>
{ "start": $StartDate,
"end": $EndDate,
"title": $Title,
"content": $Content },
<% end_control %>

]
};
...
</script>

Can anybody help? Is there a workaround?

Thanks

Avatar
Bereusei

13 June 2012 at 7:47pm Community Member, 94 Posts

Try it this way:

<div id="mycontent-container" style="display: none">
<% control children %>
<div class="StartDate">$StartDate</div>
<div class="EndDate">$EndDate</div>
<div class="Title">$Title</div>
<div class="Content">$Content</div>
<% end_control %>
</div>

And in your javascript (jQuery) you can catch the content out of your mycontent-container:
var StartDate = $('#mycontent-container .StartDate').text();
var EndDate = $('#mycontent-container .EndDate').text();
...

Avatar
Noki

13 June 2012 at 8:51pm Community Member, 6 Posts

Thanks Bereusei,
i tried this way, but it did not catch the content. It returns something like <object object>.

Furthermore I tried:

...
getElementById("Startdate")

..

<div id="Startdate">$Startdate</div>

but without success!

Any other idea?

Thanks

Avatar
jimmybrion

14 June 2012 at 8:59pm Community Member, 1 Post

Yes it is not working me to just checked

Avatar
Bereusei

6 July 2012 at 2:27am Community Member, 94 Posts

Try to change the div into spans:

<div id="mycontent-container" style="display: none">
<% control children %>
<span class="StartDate">$StartDate</span>
<span class="EndDate">$EndDate</span>
<span class="Title">$Title</span>
<span class="Content">$Content</span>
<% end_control %>
</div>

Perhaps the ".text()" doesn´t catch divs.

Avatar
kinglozzer

6 July 2012 at 2:47am Community Member, 146 Posts

Is the control returning more than one start date/end date?

If so you might need to do something along the lines of:

$('#mycontent-container .StartDate').each(function(){
alert($(this).text());
});

Though obviously not just alerting the data ;)