I have div in which I have added 5 files, using appendTo, Now I want to get the src of all these files. How do I go about getting it using jQuery. can I use .get()?
Thanks
Jean
[edit]
<div id="some">
<img src="jquery_book.jpg">JQuery Book
</div>
I need src and href.
Something like
var arrsource = new Array();
$("#yourdivid a").each(function(){
arrSource.push ( $(this).attr("href") );
});
Not using an array
$("#yourdivid a").each(function(){
var currentElemSource = $(this).attr("href");
// write your code here
});
Edit
$("#some a").each(function(){
var currentElem = $(this);
var imageSource = currentElem.prev().attr("src");
var path = currentElem.attr("href");
});
I assumed you are using anchor tags and you have got the path in href.
Not sure what you meant by the file but you can get attribute like this:
$("selector").attr('src');
Based On Your Updated Answer:
$('#some a').each(function(){
alert ($(this).attr('href'));
});
Related
I'm a beginner so please be patient. Tried this myself but can't get it to work.
I'm creating a hardcoded lexicon (no DB behind it but 26 a-z pages) - in the code the links will be "lazily" written as such:
<div class="lexicon">
<a id="letter-a"></a>
<a id="letter-b"></a>
<a id="letter-c"></a>
etc.
</div>
I then want a small jquery script before this to "get" the id, strip the "letter-" part and...
add a link to the a-Tag (ie. mylexicon/mypage-a)
add the stripped id as text for the a-Tag
The result should then be:
<a id="letter-a" href="mylexicon/mypage-a">a</a>
Got this far and bailed out:
var $a = $('.lexicon a');
$a.each(function(){
$(this).attr('href', $(this).attr('id'));
})
I also then need a line following this, that reads the last letter of the current URL (there will be 26 of course) and sets a class to the appropriate a-Tag so that I can style it. (jquery or php??)
Many thanks in advance.
There is some other ways but its a good practice to be familiar with this js functions too:
indexOf() and substr()
var _a = jQuery('.lexicon a');
_a.each(function(){
var _this = jQuery(this),// cache variable to boost speed
total_id = jQuery(this).attr('id'),// grab id attribute
altered = total_id.substr(total_id.indexOf('-')+1, total_id.length);// grab all content after '-'
_this.attr('href', 'mylexicon/mypage-'+ altered);
});
this should do what you need
$(document).ready(function(){
var $a = $('.lexicon a');
$a.each(function(){
var string = $(this).attr('id');
var lastCharacter = string.substr(string.indexOf('-')+1);
$(this).attr('href', 'mylexicon/mypage-' + lastCharacter); // add the link
$(this).text( lastCharacter ); // add the last character as text
$(this).attr('class', 'classNae-' + lastCharacter); // add unique class name for styling
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="lexicon">
<a id="letter-a"></a>
<a id="letter-b"></a>
<a id="letter-c"></a>
</div>
So i got a youtube url... which im trying to get into my database in some way...
So what i got is an image
<img src = "cat">
And i also got a youtube url
https://www.youtube.com/watch?v=AFGWnqNf6t0
Then what i want is when i click on my image i want the youtube link to be inserted into a php varible so I later can insert it into my database
$("#img").click(function(){
$.ajax({
url: "php file",
});
return false;
});
How am i supposed to call the varible since Im not posting the url?
For example like $username = $_POST['username']; when i post something but to get the youtube url instead!
You can simply use the attr property like this
HTML Code
<img src = "bilder/lenny.gif" data-action = "//https://www.youtube.com/watch?v=5dsGWM5XGdg"
alt = "lenny" id = "add" width = "40" height = "40">
jquery Code :-
$("#add").click(function(){
var dataprop = $(this).attr('data-action');
//SEND IT TO AJAX NOW
});
You can add parameters to a GET request with & and ?.
test.php?bookid=15&page=435
You can add your value with jQuery:
$("#add").click(function(){
$.ajax({
url: "spelista.php?value"= $('#idofelementwithvalue').val(),
});
return false;
});
Sending the href:
$("#add").click(function(){
$.ajax({
url: "spelista.php?value"= $('#idofelementwithhref').attr('href'),
});
return false;
});
Note: You can get teh href with attr('href')
I hope that awnsers your question!
Make sure you know what value comes from your Database the "action/href/value". Then, you need to use:
$("#add").attr("href")
In this way, you get the value of the action/href/value from the html element. Replace "href" with whatever is the attribute's name. After getting the value, you need to post it to your url by jQuery post method. Please see the following example:
$("#add").click(function(){
var videoValue = $("#add").attr("href");
$.post("spelista.php", {video: videoValue}, function(data){
alert("Data: " + data);
});
}
Hope this helps!
Just an addition to Agam's Answer:
Here, the image is wrapped inside a link tag. This is just to provide a better user experience. i.e making the image look clickable(the little click hand)
<a id="link_tag" href="https://www.youtube.com/watch?v=5dsGWM5XGdg">
<img src = "bilder/lenny.gif" alt = "lenny" id = "add" width = "40" height = "40"></a>
<script>
$("#link_tag").click(function(){
var link = $(this).attr('href');
alert('This link is '+link);
});
</script>
Also your link is invalid. remove the //
Here is the answer, so you get what i was trying to achieve..
<img src = "cat" id = "https://www.youtube.com/watch?v=AFGWnqNf6t0"
onclick = "saveVideo(this)">
Then the script.... To make the id go to php
function saveVideo(x)
{
var film = x.id;
$.post('spelista.php', {film: film},function(data){
alert(data);
})
}
And then last the php varible
$url = $_POST['film'];
var_dump($url);
And then $url is the youtube url ...
Cheers!
I have tried with below code but it is not taking current instance of class which is clicked. Instead of that it is showing content of first class which he finds within my iframe.
$(document).ready(function(){
$('#framedata').load(function(){
var iframe = $('#framedata').contents();
iframe.find(".editContent").click(function(){
$("#spanid").show();
var content_edit = iframe.find(".editContent").html();
//alert(content_edit);
//var content_edit = x.contentWindow.documentgetElementByClass('editContent').innerHTML;
$("#spanid").append("<textarea id='editor'>"+content_edit+"</textarea>");
});
//...etc...
});
});
Thanking you in advance
I can't find/think of a way to solve this problem.
I have a list of links outputted by a php script and I need to get the text value from the item (link) that's been clicked.
Here is the php part that outputs data:
$query = mysql_query("SELECT NAME FROM ccm WHERE NAME LIKE '$value%'");
while( $run = mysql_fetch_array($query)){
$name = $run['NAME'];
echo '<a id="rez_link" onClick="klik();">'.$name.'</a>';
}
And here are some of my attempts to fetch the .text() from the link that's been clicked:
var value = $('a#rez_link').text(); //this one targets every text if there are multiple search result from the query
var value = jQuery(this).find("a").text(); //this one returns nothing
So how do I do it?
Maybe I should modify the php script so that it outputs new links with id=""+i and then target them like that in jQuery or something like that.
Is there a simple way to do this?
You can't have more anchor tags with same id
echo '<a id="rez_link" onClick="klik();">'.$name.'</a>';
it's better to use class instead
echo '<a class="rez_link">'.$name.'</a>';
$(function() {
$(".rez_link").on("click",function()
{
var text = $(this).text();
alert(text);
}
});
$(function() {
$('.rez_link').bind('click', function(e) {
e.preventDefault();
var linkText = $(this).text();
$('#show').text(linkText);
});
});
It's this what you try to do ?
I have a little question.
On my site i use for few jquery functions , they get some data and transfer by GET method to php helper. This is not work when i want to use the data in another php page. For example i have jquery that store id of the anchor:
$("#rightContent .users_list li a").live('click', function() {
var id = $(this).attr('id');
$.get("getHelper",{'userId':id},function(data){
$('#user_rightContent').html(data);
});
});
And in php file returned by "getHelper" i do:
if(isset($_GET['userId'])){
$id = $_GET['userId'];
//do something
);
The anchor of the live click lead to another page displayed by another php file where i want to use this id by using helper... This is work only if i stay on same page...
Can anybody help me in this problem?
thanks for advice
add return false at the end of live('click'....
live('click', function()
{
// code ...
return false;
}
or do that
live('click', function(e)
{
// code ...
e.preventDefault();
}
You're missing the extension on the filename.
Try it with:
$("#rightContent .users_list li a").live('click', function() {
var id = $(this).attr('id');
$.get("getHelper.php",{'userId':id},function(data){
$('#user_rightContent').html(data);
});
});