How to append content to tabel with ajax? - ruby-on-rails

I need some help concerning the append call in javascript.
I have the following table in my view :
<div class="splitcontentleft">
<table class="list user_roles">
.........
</table>
</div>
How should I call here the append command in my *.js.erb file?
I do the following:
$('#user_roles_table').append('<%= #roles %>');
But it didn't work. I also tested the append command at a other div tag and that works, so I reckon the failure is the pramater '#user_roles_table', or not?

$('#user_roles_table')
tells jquery to wrap an object with the id attribute "user_roles_table". You probably want to use
$('table.user_roles')
which says select an element with tag table AND a class attribute of "user_roles" or just
$('.user_roles')
if that element is the only one with that class and you're not reusing the classnames.
See here for an exhaustive list of the selectors you can use with jquery

Related

Blog Title Scraping

I think I might just be over complicating things instead of keeping it simple.
My question is: I want to capture the title of a blog post into a prop variable, and the author who wrote it into another prop variable.
My thought would be to create a page load rule focusing only on the path of /blog. From there I would scrape the page looking for the class that defines it, and then pass it into my prop through DTM.
<div class="field field-name-title">
<h2>Online Education</h2>
<div class="field field-name-body">
<p>
<em> by Author Name</em>
</p>
</div>
</div>
I create a page rule pick my prop and set it as: div.field.field-name-title.innerText But when I set it, all I'm seeing being passed is the "div.field.field-name-title.innerText"
Am I tackling this in the wrong way?
The values you enter in a text field are literal, with the exception of %data_element% syntax, which signifies a reference to a Data Element (there are a couple of other built-in variable references, as well).
Point is, if you want to populate your Adobe Analytics variable from scraping page content, you need to create a Data Element that returns the desired value, and then reference the Data Element in the text field for the Adobe Analytics variable.
That aside, your selector is wrong. What you've done is some weird mix of css selector and javascript syntax.
Below is an example of what you can do, based on your posted HTML:
<div class="field field-name-title">
<h2>Online Education</h2>
<div class="field field-name-body">
<p>
<em> by Author Name</em>
</p>
</div>
</div>
Data Element: Article Title
First, create a Data Element to get the article title from the page, based on your html structure.
Go to Rules > Data Elements > Create New Data Element
Fill out the fields with the following:
Name: article_title
Type: CSS Selector
CSS Selector Chain: div.field-name-title h2
get the value of: text
[X] Scrub whitespace and linebreaks using cleanText
Then, click Save Changes
Data Element: Article Author
Next, create another Data Element to get the article author from the page, based on your html structure.
Go to Rules > Data Elements > Create New Data Element
Fill out the fields with the following:
Name: article_author
Type: CSS Selector
CSS Selector Chain: div.field-name-body em
get the value of: text
[X] Scrub whitespace and linebreaks using cleanText
Then, click Save Changes
Page Load Rule: Populate Variables
Finally, within the various form fields of your Page Load Rule, you can now reference your Data Elements with %data_element_name% syntax.
Tip: Once you start typing the Data Element name out (starting with % prefix), DTM will show an auto-complete dialog, listing Data Elements matched.
If you need to reference the Data Element within a javascript custom code box within the Page Load Rule, you can use the following syntax:
_satellite.getVar('data_element_name');
Where 'data_element_name' is the name of your Data Element.
Example:
s.prop1 = _satellite.getVar('article_title');
Note: Unlike the form field syntax, you should not wrap your Data Element's name with %

select an hidden element with multiple class name

In the following html code, One of the div is hidden and the other one is not.My question is
1.how to select any element with multiple class names with hidden attribute
2.how to get the inner html when the html is hidden or shown
I tried,
$('.middle-cont,.float-lft,.content-height').html()
$('.middle-cont,.float-lft,.content-height:hidden').html() //will this work
html
<div class="middle-cont float-lft content-height">
some html
</div>
<div class="middle-cont float-lft content-height" > //This div is hidden
some html123
</div>
no comma's, commas will select all elements with either of the classes, while removing the comma's will select the elements that has all the classes :
$('.middle-cont.float-lft.content-height').html()
wether or not it's hidden makes no difference for this.
FIDDLE
you don't need , to select element with multiple class....use html() to get the content.. (it doesn't matter if it is hidden or not)
$('.middle-cont.float-lft.content-height').html()
Comma using as OR operator, you dont need it here so:
$('.middle-cont.float-lft.content-height').html()
It's not matter that your element is hidden or not.
Try like this
var innerHtml = $(".middle-cont").filter(".float-lft").filter(".content-height").html();
alert(innerHtml);
Demo

Div tag issue in partial form

I've used div id tag, which used to give me drop down in index.html it worked fine, and when I used it in partial form the div part is not working.
I've used JavaScript and Ajax as well.
The code is like this..
<div title="Show More" class="show_more"></div>
If the issue is after adding a partial it is possible your code needs the addition of
locals:
or
collection:
See this post for more info on how to Pass a variable into a partial, rails 3?
the javascript was not being called, so I had to use it again in my partial form and it worked.

append to collabsible content after the header?

i am running into problem, concerning append. i ve a dynacmic collabsible, which i fill with a dynamic list. i want to append this list after the header h3 of the collabsible.
when i append it to the collabsible, it does not appear in the
<div class="ui-collapsible-content ui-collapsible-content-collapsed"> </div>
but after. therefor i get a space between the content header and the list, which i want to avoid.
i tried this:
$('some-selector > ui-collapsible-content ui-collapsible-content-collapsed') but it does not work.
any hints?
If you are trying to append inside
<div class="ui-collapsible-content ui-collapsible-content-collapsed"> </div>
Then you should use:
$('div.ui-collapsible-content.ui-collapsible-content-collapsed').append($content);
Where $content is either a jQuery object, a DOM element or a HTML string. Note the . in the selector, which specifies a class (or two).
See append.

Passing parameters to ASCX user control in MVC

I am trying to pass a string parameter to an ASCX. I want to set this to the text property of a label. In the code below it shows betwen the div tags (ignore the % signs in the html tags).
<# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" >
<%asp:Label ID="Label2" runat="server" **Text="<%# Model %> Test"** CssClass="isslabel"><%/asp:Label>
<%div><%: Model %><%/div>
However in the label tag no matter what I put between the angle brackets (bit it bold) in the text tag I cannot get the parameter to appear. I have tried <%: Model %> to no avail. Is the issue that the code block is inside quotes and am I just missing some character?
Why are you trying to use a web forms user control with MVC?
Assuming the model is the string you want to display and you are passing this through correctly, along the lines of
<% Html.RenderPartial("MyUserControlView", "My String To Display"); %>
In your "parent" page, you will be able to do the following in your ascx:
<%= Html.Label(Model) %>
Instead of <asp:label...
Update
If you need to specify then you have a number of options, you could wrap the Html.Label call in a div and specify the class of the div (updating your css accordingly), you could use a display template, or simply explicitly use the Html like the following:
<label for="someIdThatICouldUseAnotherHtmlExtensionMethodToGet"><%: Model %></label>
The key problem with your code (as now also pointed out in the comments by #mare) is that you are trying to use a web forms control in an MVC view.

Resources