[jQuery] Re: Insert variable into a selector
This is the JS code: $('.makeFavorite').click(function() { $.ajax({ type: POST, url: make_favorite.php, data: id= + $(this.id), success: function(msg) { alert('Data saved: ' + msg); $('h1 a#' + this.id + ' img').attr({src : images/+msg+.png}); } }); return false; }); The HTML code: h1{$ent.firma} a href={$ent.id} class=makeFavorite id={$ent.id} title=Save favoriteimg src=images/{if $ent.favorit == 1}favorites.png{else}favorites2.png{/if} width=16 height=16 alt= //a/h1 Both variables don't do what what I expect them to do. The first doesn't find the correct target/id and the seccond doesn't insert the image correctly. But if I write down the id and the true path it works fine. What do I have to change? On 22 Sep., 05:42, ricardobeat [EMAIL PROTECTED] wrote: I can't get it either, what are you trying to accomplish? $('h1 a#' + this.id') is not logical, you first need to reference some a element to get it's ID, but in doing that you already wrote the ID... On Sep 21, 11:16 pm, FrenchiINLA [EMAIL PROTECTED] wrote: i think your problem is this.id, you have to show the entire code in order for us to see what does this mean. try just to add a alert for example to see what you get for this.id On Sep 21, 7:38 am, suntrop [EMAIL PROTECTED] wrote: HI there, I want to insert two variables into the selector and an attribute. But it doesn't work. $('h1 a#' + this.id + ' img').attr({src : images/ + msg + .png}); I looked through various tutorials but couldn't find an answer to this. The first variable comes from the object's (a element) id and the seccond is a response from an php script. Can somebody please help me?
[jQuery] Re: Insert variable into a selector
According to the docs, 'this' within a success callback is the options object, so 'this.id' doesn't mean anything useful: http://docs.jquery.com/Ajax/jQuery.ajax#options Also, $(this.id) probably isn't anything useful either. Maybe try this (untested): $('.makeFavorite').click(function() { var id = this.id; $.ajax({ type: POST, url: make_favorite.php, data: id= + id, success: function(msg) { alert('Data saved: ' + msg); $('#' + id + ' img').attr({src : images/+msg+.png}); } }); return false; }); Hope it helps. --Erik On Mon, Sep 22, 2008 at 1:16 AM, suntrop [EMAIL PROTECTED] wrote: This is the JS code: $('.makeFavorite').click(function() { $.ajax({ type: POST, url: make_favorite.php, data: id= + $(this.id), success: function(msg) { alert('Data saved: ' + msg); $('h1 a#' + this.id + ' img').attr({src : images/+msg+.png}); } }); return false; }); The HTML code: h1{$ent.firma} a href={$ent.id} class=makeFavorite id={$ent.id} title=Save favoriteimg src=images/{if $ent.favorit == 1}favorites.png{else}favorites2.png{/if} width=16 height=16 alt= //a/h1 Both variables don't do what what I expect them to do. The first doesn't find the correct target/id and the seccond doesn't insert the image correctly. But if I write down the id and the true path it works fine. What do I have to change? On 22 Sep., 05:42, ricardobeat [EMAIL PROTECTED] wrote: I can't get it either, what are you trying to accomplish? $('h1 a#' + this.id') is not logical, you first need to reference some a element to get it's ID, but in doing that you already wrote the ID... On Sep 21, 11:16 pm, FrenchiINLA [EMAIL PROTECTED] wrote: i think your problem is this.id, you have to show the entire code in order for us to see what does this mean. try just to add a alert for example to see what you get for this.id On Sep 21, 7:38 am, suntrop [EMAIL PROTECTED] wrote: HI there, I want to insert two variables into the selector and an attribute. But it doesn't work. $('h1 a#' + this.id + ' img').attr({src : images/ + msg + .png}); I looked through various tutorials but couldn't find an answer to this. The first variable comes from the object's (a element) id and the seccond is a response from an php script. Can somebody please help me?
[jQuery] Re: Insert variable into a selector
Hi Can you show more code? I think showing an alert would be a good idea. On Sep 22, 12:38 am, suntrop [EMAIL PROTECTED] wrote: HI there, I want to insert two variables into the selector and an attribute. But it doesn't work. $('h1 a#' + this.id + ' img').attr({src : images/ + msg + .png}); I looked through various tutorials but couldn't find an answer to this. The first variable comes from the object's (a element) id and the seccond is a response from an php script. Can somebody please help me?
[jQuery] Re: Insert variable into a selector
you concatenate a string with an object.. On 22 Sep., 10:16, suntrop [EMAIL PROTECTED] wrote: This is the JS code: $('.makeFavorite').click(function() { $.ajax({ type: POST, url: make_favorite.php, data: id= + $(this.id), success: function(msg) { alert('Data saved: ' + msg); $('h1 a#' + this.id + ' img').attr({src : images/+msg+.png}); } }); return false;}); The HTML code: h1{$ent.firma} a href={$ent.id} class=makeFavorite id={$ent.id} title=Save favoriteimg src=images/{if $ent.favorit == 1}favorites.png{else}favorites2.png{/if} width=16 height=16 alt= //a/h1 Both variables don't do what what I expect them to do. The first doesn't find the correct target/id and the seccond doesn't insert the image correctly. But if I write down the id and the true path it works fine. What do I have to change? On 22 Sep., 05:42, ricardobeat [EMAIL PROTECTED] wrote: I can't get it either, what are you trying to accomplish? $('h1 a#' + this.id') is not logical, you first need to reference some a element to get it's ID, but in doing that you already wrote the ID... On Sep 21, 11:16 pm, FrenchiINLA [EMAIL PROTECTED] wrote: i think your problem is this.id, you have to show the entire code in order for us to see what does this mean. try just to add a alert for example to see what you get for this.id On Sep 21, 7:38 am, suntrop [EMAIL PROTECTED] wrote: HI there, I want to insert two variables into the selector and an attribute. But it doesn't work. $('h1 a#' + this.id + ' img').attr({src : images/ + msg + .png}); I looked through various tutorials but couldn't find an answer to this. The first variable comes from the object's (a element) id and the seccond is a response from an php script. Can somebody please help me?- Zitierten Text ausblenden - - Zitierten Text anzeigen -
[jQuery] Re: Insert variable into a selector
Thanks Erik. It helped! The id is inserted correctly. But the image isn't replaced correctly. What code should I sow you more Makisa? The other code isn't affected with this. What do you mean? On 22 Sep., 10:26, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: you concatenate a string with an object.. On 22 Sep., 10:16, suntrop [EMAIL PROTECTED] wrote: This is the JS code: $('.makeFavorite').click(function() { $.ajax({ type: POST, url: make_favorite.php, data: id= + $(this.id), success: function(msg) { alert('Data saved: ' + msg); $('h1 a#' + this.id + ' img').attr({src : images/+msg+.png}); } }); return false;}); The HTML code: h1{$ent.firma} a href={$ent.id} class=makeFavorite id={$ent.id} title=Save favoriteimg src=images/{if $ent.favorit == 1}favorites.png{else}favorites2.png{/if} width=16 height=16 alt= //a/h1 Both variables don't do what what I expect them to do. The first doesn't find the correct target/id and the seccond doesn't insert the image correctly. But if I write down the id and the true path it works fine. What do I have to change? On 22 Sep., 05:42, ricardobeat [EMAIL PROTECTED] wrote: I can't get it either, what are you trying to accomplish? $('h1 a#' + this.id') is not logical, you first need to reference some a element to get it's ID, but in doing that you already wrote the ID... On Sep 21, 11:16 pm, FrenchiINLA [EMAIL PROTECTED] wrote: i think your problem is this.id, you have to show the entire code in order for us to see what does this mean. try just to add a alert for example to see what you get for this.id On Sep 21, 7:38 am, suntrop [EMAIL PROTECTED] wrote: HI there, I want to insert two variables into the selector and an attribute. But it doesn't work. $('h1 a#' + this.id + ' img').attr({src : images/ + msg + .png}); I looked through various tutorials but couldn't find an answer to this. The first variable comes from the object's (a element) id and the seccond is a response from an php script. Can somebody please help me?- Zitierten Text ausblenden - - Zitierten Text anzeigen -
[jQuery] Re: Insert variable into a selector
But the image isn't replaced correctly Perhaps try changing this line instead $('#' + id + ' img').attr(src, images/+msg+.png); There's no reason why that (or your line for that matter) wouldn't work as long as the selector finds something and the value of the src is a valid path on your server
[jQuery] Re: Insert variable into a selector
When write: $('#' + id + ' img').attr(src, images/favorites.png); … it works. alert (msg) says favorites. There must be something wrong with msg. If the variable is defined before var favimg = favorites; and I insert favimg it works just fine. What could be wrong with msg? msg is what I echo back in my PHP code. I don't know if this is the appropriate way. On 22 Sep., 13:34, MorningZ [EMAIL PROTECTED] wrote: But the image isn't replaced correctly Perhaps try changing this line instead $('#' + id + ' img').attr(src, images/+msg+.png); There's no reason why that (or your line for that matter) wouldn't work as long as the selector finds something and the value of the src is a valid path on your server
[jQuery] Re: Insert variable into a selector
are you sure there is no white space getting returned in your echo? if you have a line break (which you can't see in the returned value) that could be throwing off the img src tag maybe wash the returned value through a function stripping off leading and trailing whitespace http://www.google.com/search?hl=enq=javascript+strip+whitespace
[jQuery] Re: Insert variable into a selector
No one could possibly have any idea what's wrong there (other than a lucky guess), because we're not seeing your actual code. Forget about PHP for the moment and look at what you get back from a View Source in the browser. That's all the browser and its JavaScript interpreter know about - your PHP source is out of the picture at that point. Do you see what's wrong there? If not, then please post a link to a live test page (not just a code snippet) and I'm sure someone will be able to spot the problem. -Mike From: suntrop When write: $('#' + id + ' img').attr(src, images/favorites.png); . it works. alert (msg) says favorites. There must be something wrong with msg. If the variable is defined before var favimg = favorites; and I insert favimg it works just fine. What could be wrong with msg? msg is what I echo back in my PHP code. I don't know if this is the appropriate way. On 22 Sep., 13:34, MorningZ [EMAIL PROTECTED] wrote: But the image isn't replaced correctly Perhaps try changing this line instead $('#' + id + ' img').attr(src, images/+msg+.png); There's no reason why that (or your line for that matter) wouldn't work as long as the selector finds something and the value of the src is a valid path on your server
[jQuery] Re: Insert variable into a selector
Hi Michael, I didn't want to screw things up with the complete code, because I thought it is a pretty simple thing :-) So here is the whole code: (I haven't a live page, it is only on my local machine) !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd; html xmlns=http://www.w3.org/1999/xhtml; xml:lang=de head meta http-equiv=content-type content=text/html; charset=utf-8 / titleWerbetrommel/title link rel=stylesheet type=text/css href=http:// yui.yahooapis.com/2.5.2/build/reset-fonts-grids/reset-fonts- grids.css link rel=stylesheet type=text/css href=http:// yui.yahooapis.com/2.5.2/build/base/base-min.css link rel=stylesheet type=text/css href=css/master.css script type=text/javascript src=js/jquery-1.2.6.pack.js/ script script type=text/javascript {literal} $(document).ready(function() { $('.customer').hide(); $(h1).click(function() { $(this).next().slideToggle(); }); $('#displayAll').click(function() { $('.customer').each(function() { $(this).show(); }); return false; }); $('#displayNone').click(function() { $('.customer').each(function() { $(this).hide(); }); return false; }); // Make Favorite $('.makeFavorite').click(function() { var id = this.id; var favimg = favorites; $.ajax({ type: POST, url: make_favorite.php, data: id= + $(this.id), success: function(msg) { alert('Data saved: ' + msg); $('h1 a#'+id+' img').attr({src : images/+favimg+.png}); } }); return false; }); }); {/literal} /script /head body div id=wrapper div id=header img src=/images/core/logo.png width= height= title= alt= / div id=searchRow form action= id=searchBox method=post input type=text / /form /div div id=alphabetRow ul lia/li lib/li liy/li liz/li /ul /div div id=addRow img src=images/add.png width=32 height=32 title=Neuen Interessenten hinzufügen alt=Hinzufügen / /div /div!-- end #header -- div id=listing div id=control a href=# id=displayAll title=Alle User vollständig einblendenAlle einblenden/a - a href=# id=displayNone title=Alle User ausbendenAlle ausblenden/a /div {foreach from=$eintrag item=inhalt} h1{$inhalt.firma} a href={$inhalt.id} class=makeFavorite id={$inhalt.id} title=Als Favorit speichernimg src=images/{if $inhalt.favorit == 1}favorites.png{else}favorites2.png{/if} width=16 height=16 alt= //a/h1 div id=customer_{$inhalt.id} class=customer div class=user h2Empfänger/h2 p{$inhalt.name}/p p{$inhalt.adresse|nl2br}/p p{$inhalt.email}br /a href={$inhalt.website} title={$inhalt.website}{$inhalt.website}/a/p /div!-- end .user --
[jQuery] Re: Insert variable into a selector
Can you show more code? The first variable comes from the object's (a element) id doesn't make any sense
[jQuery] Re: Insert variable into a selector
i think your problem is this.id, you have to show the entire code in order for us to see what does this mean. try just to add a alert for example to see what you get for this.id On Sep 21, 7:38 am, suntrop [EMAIL PROTECTED] wrote: HI there, I want to insert two variables into the selector and an attribute. But it doesn't work. $('h1 a#' + this.id + ' img').attr({src : images/ + msg + .png}); I looked through various tutorials but couldn't find an answer to this. The first variable comes from the object's (a element) id and the seccond is a response from an php script. Can somebody please help me?
[jQuery] Re: Insert variable into a selector
I can't get it either, what are you trying to accomplish? $('h1 a#' + this.id') is not logical, you first need to reference some a element to get it's ID, but in doing that you already wrote the ID... On Sep 21, 11:16 pm, FrenchiINLA [EMAIL PROTECTED] wrote: i think your problem is this.id, you have to show the entire code in order for us to see what does this mean. try just to add a alert for example to see what you get for this.id On Sep 21, 7:38 am, suntrop [EMAIL PROTECTED] wrote: HI there, I want to insert two variables into the selector and an attribute. But it doesn't work. $('h1 a#' + this.id + ' img').attr({src : images/ + msg + .png}); I looked through various tutorials but couldn't find an answer to this. The first variable comes from the object's (a element) id and the seccond is a response from an php script. Can somebody please help me?