How to get 'href' value of <a> element with jquery - jquery-mobile

How to get href value of <a> element and store it to a variable with jquery mobile?
This is my snippet code:
$('a').live('click', function() {
var href = $(this).attr('href');
console.log("href value: |"+href);
});
Here is sample:
Link
In my log cat (btw I am using jquery mobiel for phonegap), it returns : href value: |#
What i want is, something like this in my logcat:
href value: | 117.xx.xx.xx/timesheet/file.pdf
I'm using phonegap 2.9

Based on the symptoms you describe, it seems as though the a tag firing the click event has an href of #. Possibly some other tag wrapping your intended anchor? Try a more specific selector and see if that works, maybe something like:
$('#myLink').live('click', function() {
var href = $(this).attr('href');
console.log("href value: |"+href);
});
With this for a link:
Link
If it still gives you a #, then is it possible that the href value is populated after your click event is firing (possibly by some other javascript)?

Finally, solved. After frustating with jquery 'live' and 'on', I'm using plain Javascript function to get value of link in attribute: getAttribute('href'). And that worked!! Anyway, thank's for all the answer.

Related

how to transfer data from <a> tag to popup dialog in jquery mobile?

i dynamically generate this html code to delete an item with an id=3 for example:
"<a href='javascript:delete('" + item.id + "')>";
when i click this, it will execute delete('3');i change it as:
<a href='#delete' data-rel='popup' data-position-to='window' data-transition='pop'>
and add a dialog for this tag:
<div data-role='popup' id='delete'>
<a href='javascript:delete(item.id)' data-role='button'>delete</a>
</div>
how to transfer the item's id to this popup dialog's tag, any suggestion?
I feel like you might be going through the wrong way to achieve this. Some things to change :
delete is a JavaScript keyword. You cant use it as a function.
Don't use the onclick attribute. It results in duplication. Instead, you could use a click event for repetitive actions.
You seem to have gotten the idea to create multiple popups (one for each click of the anchor tag). I think one would do.
Now, in correlation with whatever I've just put down, here's some sample code.
HTML
<a href='#' class='delete' data-num='" + i + "'>Delete me</a>
(Note the data-num attribute in the HTML, the addition of class attribute and the removal of onclick in your code)
It could be replaced by JS which looks like this :
$(this).on("click", ".delete", function (e) {
//prevent default action
e.preventDefault();
//take the id value
var id = $(this).data("num");
//send that value to the popup
$("#delete").find("span").html(id).end().popup("open");
});
A demo fiddle for you to look at : http://jsfiddle.net/hungerpain/AxGde/2/

jquery ui 1.10.2 tabs - link to a tab from another page or website

I'm using the most recent JQuery UI Tabs (1.10.2). http://jqueryui.com/tabs/
I need to be able to link to individual tabs from external pages. Maybe the more correct way to say it would be to say that I need to be able to change the initially active tab via a bookmarble link.
I know how to set the active index so that #tabs-3 is the active tab
$( "#tabs" ).tabs({ active: 5 });
But I need to know how to change the value for .tabs({ active }) with the url hash so that a link of "tabs-page.html#tabs-3" will load the third tab of "tabs-page.html" by changing .tabs({ active }) to "2" (since it is a zero-based integer).
I'm really more of an html/css designer and a novice to JQuery/JQuery UI, please and thank you for your help. I've searched and found fixes for earlier versions and alternate libraries like JQuery Tools, but nothing for JQuery 1.10.2. I've found ways to link to the section and then reset the window location, but that results in a lot of "jumpiness" as the browser switches between window locations. If there is another page with this fix please link in the comments. THANKS SO MUCH!!!
You will need to read the value of the hash within your jQuery. Some good information can be found here Getting URL hash location, and using it in jQuery
var url = "http://site.com/file.htm#3";
var hashValue = url.substring(url.indexOf('#')).replace('#',''); // '3'
Once you have this, you will be able to set the active tab on the jqueryUI Tabs
$('#tabs').tabs( "option", "active", hashValue );
You would need to do all of this when the page initially loads, so within a $(function(){ ... });
Update
Here is the full code;
<script>
$(function () {
// run the jquery ui plugin
$('#tabs').tabs();
// grab the url
var url = document.URL;
// grab the value of the hash
var hashValue = url.substring(url.indexOf('#')).replace('#', '');
// check to make sure it is a number
if (!isNaN(hashValue)) {
// set the active tab
$('#tabs').tabs("option", "active", hashValue);
}
});
</script>

Append html to div not working on cordova iOS

I am using cordova 2.2.0 with Xcode 4.5.2. On one of my pages I have a select component and once a particular option is selected I want to display a table within a div. My html looks like this: <div class="hero-unit" id="#facprog"></div>
The javascript/jquery looks like this:
<pre><code>
$(function() {
$('#selFaculty').change(function() {
var facultyName = $('#selFaculty').val();
var progDetails = app.fillFacultyProgramme(facultyName);
alert(progDetails);
$('#facprog').append(progDetails);
});
});
</code></pre>
Note that #selFaculty is the id of the select object. However, when the select option changes, everything in javascript executes except for the append. Nothing shows up in the div. Is there a different way to add html element to a div in jquery under cordova 2.2.0 in iOS?
Thanks in advance,
José
With jquery you can refresh a div.
This is a good example:
<script>
var auto_refresh = setInterval(
function()
{
$('#loaddiv').fadeOut('slow').load('reload.php').fadeIn("slow");
}, 20000);
</script>
and now you need to put the div in the body section of your page
<div id="loaddiv">
</div>
from http://designgala.com/how-to-refresh-div-using-jquery/
and there is another example here:
refresh div with jquery
The first example is useful when you need to load a script and refresh and the second one is useful when you need to make a change, e.g. append html and then refresh as in your case.

mobile.changepage requiring page refresh

I'm using $.mobile.ChangePage() to navigate from one HTML page to another. But the contents of the page is not changing. while doing so the page URL changes but the new page is not loaded. it requires to be refreshed to load.
you should use JavaScript function to change the page..using
window.location = 'your page ';
hope it will resolve your problem !
In the code that you provided in the comments above, you are likely getting an error stating that next_page is not defined. Given that you are dealing with jQuery Mobile, try assigning the click handler slightly differently:
Change the definition of the link to something like this:
<a id="btnNextPage" href="#" >Details</a>
You don't have to give it an id - you could just use selectors to identify the link in the following javascript:
// the event handler
function next_page() {
$.mobile.changePage("http://www.google.com", {transition:"slide"});
}
// assign the click event when the DOM is ready
$(function() {
$("#btnNextPage").click ( next_page );
});

CSS/HTML: Disable link "hover" text

You know how when you have your mouse over a link, in most browsers, it shows the link in the lower left corner (aka chrome) or in the status bar? How can I disable this?
The only way to do this is to remove the data in 'href', and change it to a javascript onclick where you set the window.location to the url you want.
Go To SO
becomes
<a style="cursor: pointer" onclick="javascript: window.location = 'http://www.stackoverflow.com/';">Go To SO</a>
another idea: use a redirector.
Set the link to your own page (aspx) and in that page you do a Response.Transfer. When using aspx, you can use attributes (in the querystring) if you like, to use it for multiple links. This way the user still knows it is a link, but can't see the actual URL when hovering.
I got the same issue today .. and here is a out of the box way to solve it:
Just replace the <a> with a <span> and hide the address in a hidden component,
Then use Jquery to create the page redirect / Ajax.
HTML:
<span class="fake-link" >
<span class="url" style="display:none;">www.my-url.com</span>
Go to My-URL page
</span>
Jquery:
$(function(){
$('.fake-link').on('click', function(e){
var url = $(this).find('.url:first').html();
window.location = url;
});
});
Change the onclick event:
Link
<script type="text/javascript">
function changeOnClick() {
document.getElementById("linkid").onclick=function(e) {
location.href="http://www.your-site.com";
return false;
}
}
window.onload=changeOnClick;
</script>
You can change the "#" to whatever you want the status bar to show.

Resources