Creating a Hovercard with jQuery Hovercard and custom data attributes - jquery-plugins

I need some help about creating more HoverCards with this plugin (download). I just created a code demo on JSFiddle. Do you have any recommendations about this?
JavaScript:
$('.babe-hover').hovercard({
detailsHTML: $(this).attr('data-control').html(),
width:278
});
HTML:
<ul class="demo">
<li>
<span class="babe-hover" data-control="control-01">William Johnson</span>
<div id="control-01" style="display: none;">
<p class="s-desc">Address: 64 Newman Street.</p>
<ul class="s-stats">
<li>Tweets<br><span class="s-count">1337</span></li>
</ul>
</div>
</li>
<li>
<span class="babe-hover" data-control="control-02">Hanson Thomas</span>
<div id="control-02" style="display: none;">
<p class="s-desc">Address: 64 Newman Street.</p>
<ul class="s-stats">
<li>Tweets<br><span class="s-count">1337</span></li>
</ul>
</div>
</li>
​

Now it working with some help from the author
$('.babe-hover').each(function(){
var $this = $(this),
myControlId = $this.attr('data-control'),
htmlForHovercard = $('#'+ myControlId).html();
$this.hovercard({
detailsHTML: htmlForHovercard,
width:278
});
});
anyway thank #egasimus for ur suggest :)

I suppose you're asking: why doesn't this work?
You're trying to call the .html() method on what $(this).attr('data-control') returns. $(this).attr('data-control'), however, only returns a string, and you need to obtain the corresponding element in order to use .html(). The following code works for me:
$("#" + $(this).attr('data-control')).html()
I.e., "select the element whose id equals this element's data-control attribute, and call .html() on it."

Related

using variables in HtmlXPathSelectors

I am using Scrapy and have run into a few places where it would be nice to use variables, but I can't figure out how. Meaning if I have some long string it would be nice to store it in a variable long_string and then select for it: hxs.select('\\div[#id=long_string]').
I'm sure this is supported by Scrapy and I just can't figure it out as it wouldn't make sense for you to always have to hard-code the string in.
Update:
So for the sample text below I want to extract the div where id="footer":
<div id="footer">
<div id="footer-menu">
<div class="region-footer-menu">
<div id="block-menu-menu-footer-menu" class="block-menu">
<div class="content">
<ul class="menu">
<li class="first leaf">FAQs</li>
<li class="leaf">Media</li>
<li class="leaf">Partners</li>
<li class="last leaf active-trail">Jobs</li>
</ul>
</div>
</div>
<div id="block-block-52" class="block block-block">
<div class="content">
<p>SUPPORT</p>
</div>
</div>
</div>
</div>
</div>
We initialize hxs = HtmlXPathSelector(response) for all the below segments.
The following code selects only the first div:
hxs.select('//div[#id=concat("foot","er")]')
This code selects nothing but gives no error:
hxs.select('//div[#id="foot"+"er"]')
Both of the below code segments select nothing and give no errors:
long_string = "foot"
hxs.select('//div[#id=concat(long_string,"er")]')
hxs.select('//div[#id=long_string]')
I would like to be able to do either of the bottom two methods and return the desired results.
Assuming + works for string concatenation in Scrapy, this should work:
hxs.select('//div[#id="' + long_string + '"]')
I'm not familiar with Scrapy, but I don't think you'll be able to select a div that doesn't exist.
have you tried?
hxs.select('\\div[#id="' + long_string_variable + '"]')

ViewBag does not show messege in view

I created 2 div's inside a view.
to display content inside that i have used partial view and to show errors i am using Viewbag and displayed it as follows:
<div id="tabs">
<ul>
<li>Publish Exhibition</li>
<li>Edit Existing Exhibition</li>
</ul>
<div id="tabs-1" style=" width:900px; height:400px;">
<font color="red">#ViewBag.msg1</font>
#{Html.RenderPartial("PubExhi", Model);}
</div>
<div id="tabs-2">
#{Html.RenderPartial("EditExhi");}
</div>
</div>
I'm calling this view as
ViewBag.msg1 = "record not updated!!!!";
modelList = setIndex();
return View("Index", modelList);
evry thing is working fine except the viewbag
it is not displaying any msgs
When i debugged it, i can see the viewbag containing appropriate value
but it is not displaying on the page
I also tried to display it from the partial view but not getting any message
Can any body help me
Use #Html.Raw(ViewBag.msg1) to display.....
It should work, i tried exactly similar thing and it is working for me -
<div id="tabs">
<ul>
<li>Tab 1</li>
<li>Tab 2</li>
</ul>
<div id="HWTab">
<font color="red">#ViewBag.msg</font>
#{Html.RenderPartial("_HardwareAssetView", Model.hardwareAssetVM);}
</div>
<div id="AdminTab">
#{Html.RenderPartial("_AdminAssetView", Model.adminAssetVM);}
</div>
you need to assign it to the view you are sending in your server method: let's say my viewresult is Details, but you are sending view DetailingList, then you need to assign this view's viewbag the message you want:
var partialViewRes = new PartialViewResult();
partialViewRes.ViewName = "DetailingList";
partialViewRes.ViewData.Model = list;
partialViewRes.ViewBag.ErrorMessage = "Error on loading";
Hope this helps someone.

Accessing a div element in an array of li elements

I am trying to access a div in an li array
<ul>
<li class="views-row views-row-1 views-row-odd views-row-first">
<div class="news-item">
</li>
<li class="views-row views-row-2 views-row-even">
<li class="views-row views-row-3 views-row-odd">
<div class="news-item">
<div class="image">
<div class="details with-image">
<h2>
<p class="standfirst">The best two-seat </p>
<div class="meta">
<div class="pub-date">26 April 2012</div>
<div class="topic-bar clearfix">
<div class="topic car_review">review</div>
</div>
</div>
</div>
</div>
</li>
I am trying to access the "div class="topic car_review">car review "and get its text.
The reason I am specifically using that text is that, depending on what the text is it would enter specific steps.
Code that I am using is
#topic = #browser.li(:class => /views-row-#{x}/).div(:class,'news-item').div(:class,'details').div(:class,'meta').div(:class,/topic /).text
The script was working fine before and suddenly it has stopped working and is just not able to get the div(:class,'news-item').
The error message I get is
unable to locate element, using {:class=>"news-item", :tag_name=>"div"} (Watir::Exception::UnknownObjectException)
I tried div(:class => /news-/) but still its just not able to find that element
I am really stuck!!!
I assume that when you are doing li(:class => /views-row-#{x}/), the x means you are iterating over all rows? If so, then your script will fail on the row-2 since it does not contain the news-item div (resulting in the error that you see).
If there is only one of these 'topic car_review' div tags, you can just do:
#topic = #browser.div(:class, 'topic car_review')
Update - Iterating over each LI:
If you need to iterate over each LI, then you could do:
#browser.lis.each do |li|
#topic = li.div(:class, 'topic car_review').text
end

Show dynamic li with jquery mobile style in Ajax call

I am having left side ul and right side ul. When i am clicking left side li making an ajax call in that ajax call i am adding a dynamic li in right side ul. The content was coming but the jquery mobile design was missing. I searched i got a solution like add $("#ul_id").listview('refresh') but it's not working for me.
$.post('/app/Unit/unit_detail', {item_id : task_id},
function(data){
$('#right_ul').html(data);
$("#right_ul").listview('refresh');
});
In my unit_detail.erb i am having the following code
<li class="showDetails" style="list-style:none;" data-theme="b">
<a href="#">
<div class="ui-grid-b">
<div class="ui-block-a"><div><%= unit_detail.name %></div></div>
<div class="ui-block-b"><div><%= unit_detail.description %> </div></div>
<div class="ui-block-c"><div><%= unit_detail.accessories %> </div></div>
</div>
</a>
</li>
In some place i found first find the id of ul in div and refresh the list view
$("#rigth_pane_content_footer").find("#right_ul").listview('refresh');
I tried that also not working.
In my page i am having the following
<div id="rigth_pane_content_footer">
<ul data-role="listview" id="right_ul">
</ul>
</div>
Any suggestions?
try this
$.post('/app/Unit/unit_detail', {item_id : task_id},
function(data){
var container = document.getElementById('right_ul');
var new_element = document.createElement('li');
new_element.innerHTML =data;
container.appendChild(new_element);
$(container).listview("refresh");
});

Prototype function to set onclick event observer in checkboxes

I want to set an observer for each of the following checkboxes:
<div id="listr">
<ul id="lr" style="list-style-type: none">
<p class="tir">Text 1</p>
<li class="ro"><input type="checkbox" name="r1" id="r1"/>A1</li>
<li class="ro"><input type="checkbox" name="r2" />A2</li>
<p class="tir">Text 2</p>
<li class="rubro"><input type="checkbox" name="r3" />B1</li>
<p class="tir">Text 3</p>
<li class="ro"><input type="checkbox" name="r4" />B2</li>
</ul>
</div>
It works if I write one observer per checkbox, but I want to do it in a short fashion, so I need something like
$$('listr.lr.li.input').invoke('observe',click,function(field) {
alert(this.name + ' clicked ' + this.checked);
// other stuff ...
});
Which is not working
Thanks in advance
Try using a valid CSS3 selector:
$$('#listr input[type="checkbox"]').invoke('observe','click',function(field) {
alert(this.name + ' clicked ' + this.checked);
// other stuff ...
});
http://prototypejs.org/doc/latest/dom/dollar-dollar/

Resources