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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Child Control in Java Script ?


Go to End


6 Posts   2558 Views

Avatar
Noki

Community Member, 6 Posts

10 June 2012 at 2:18am

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

Community Member, 96 Posts

13 June 2012 at 7:47pm

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

Community Member, 6 Posts

13 June 2012 at 8:51pm

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

Community Member, 1 Post

14 June 2012 at 8:59pm

Yes it is not working me to just checked

Avatar
Bereusei

Community Member, 96 Posts

6 July 2012 at 2:27am

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

Community Member, 187 Posts

6 July 2012 at 2:47am

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 ;)