Jquery, printing to a specific class;
Author |
Message |
nonamedude
|
Posted: Wed Jun 08, 2011 12:38 pm Post subject: Jquery, printing to a specific class; |
|
|
Hey guys;
Im having a bit of a problem when printing out something to html using jquery. I want to print to a specific class which has a specific id but so far I can only print to a specific id.
code: | $('a.commentup').bind('click',function()
{
var myid = $(this).attr('id');
var myid2 = $('a.common').attr('id');
var newValue = parseInt(myid) + 1000;
$.get(
"http://localhost/hurdit/includes/comment.php?id="+myid+"&storyID="+myid2,
null,
function(data) {
var value = data;
var divId = parseInt(myid) + 2000;
if( value == 'log' ) {
alert ("You have to be logged on to rate stories");
}
else{
$( "#" + myid + " img").attr("src","images/thumbup_blue.png");
$( "#" + (newValue) + " img").attr("src","images/thumbdown_gray.png");
$( "#" + divId).html(value);} // <-------------------------------------------------------this is the important part/question, This prints out to an id which is divId
and it does not care about the class. I am wondering if i can specify
which class i want it to print out too.
}
)
} |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
2goto1
|
Posted: Wed Jun 08, 2011 1:17 pm Post subject: RE:Jquery, printing to a specific class; |
|
|
jQuery selectors such as $("#myDiv")) follow the same rules as CSS3 selectors in general. $(".myClass") finds all elements with the class named myClass. $("#myId") finds all elements with the id named myId (which should in theory be one in well-formed html/xhtml).
So just use the . rather than #.
There are many good resources online that can help you. See http://docs.jquery.com/Tutorials:Getting_Started_with_jQueryfor an example of some basic jQuery selectors |
|
|
|
|
|
nonamedude
|
Posted: Wed Jun 08, 2011 9:26 pm Post subject: Re: Jquery, printing to a specific class; |
|
|
Uhmm, so I am still unclear on how to achieve this. I tried the tutorial page, but I think that it has been taken down.
so far ive tried
common.$( "#" + divId).html(value);
and that does not seem to do the trick :/ . |
|
|
|
|
|
Amailer
|
Posted: Thu Jun 09, 2011 6:47 am Post subject: RE:Jquery, printing to a specific class; |
|
|
You cannot specifically pick a class unless it was called say on a click event or something.
Classes can be used multiple times on a page and therefore are not unique.
IDs however can (should) only be used once on a page and therefore are unqiue.
You would have to tag each class with a unique id.
However I suppose you could also use the find()
method to search for the nearest .className.
Mind posting the HTML for this? Might be easier to see what exactly you are trying to accomplish. |
|
|
|
|
|
nonamedude
|
Posted: Thu Jun 09, 2011 11:03 am Post subject: Re: Jquery, printing to a specific class; |
|
|
code: | $('a.common').bind('click',function() // checks if the <a> tag which belongs to the class of common if clicked upon
{
var myid = $(this).attr('id'); //gathers the id of of the object that was clicked in the <a> tag
var newValue = parseInt(myid) + 10;
$.get( // sends the gathered id to the URL specified below to be processed and data to be changed in the database
"http://localhost/hurdit/includes/upvote_story.php?id="+myid,
null,
function(data) { // this processes the data returned by the php script and prints out the appropriate results
var value = data;
var divId = parseInt(myid) + 50;
if( value == 'log' ) {
alert ("You have to be logged on to rate stories");
}
else{
$( "#" + myid + " img").attr("src","images/thumbup_blue.png"); // changes the voting image
$( "#" + (newValue) + " img").attr("src","images/thumbdown_gray.png"); // changes the voting image
$( "#" + divId).html(value);} // prints out the final result to the browser
}
)
} |
This is where i print the data out
code: | echo " <div id=$divId class = 'common' >$votes[$justNews]</div> |
As you can see, both of them have the same class, but I need to find a way to specifically print it out to this class, because if I have overlapping id's they print out to both. |
|
|
|
|
|
Amailer
|
Posted: Thu Jun 09, 2011 12:19 pm Post subject: RE:Jquery, printing to a specific class; |
|
|
Are you saying that for
$divId can be used twice on the same page (meaning, its not unique)?
In any case, is "a.common" inside of this <div id=$divId class = 'common' > ?
If so you can try and use the parent() method
code: |
$(this).parent('.common').html(value);
|
|
|
|
|
|
|
|
|