Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript jQuery
Hi,
I want to get a class name from my HTML object in JavaScript (jQuery) and write it into a variable. I was trying to find it in the documentation for jQuery, but I wasn't successful. Next, I want to get source of an image (path to the image) or a text of a paragraph (p).
 
How can I get class name from my HTML object? And how can I get image source or text from paragraph?
 
Please, help me.
 
Here, I am inserting the elements: if Textbox is empty I insert image, else I insert paragraph.
if ($("#Textbox1").val().toString() == "") {
            //if user has not written any text, inserts image to screen.
                $("#platno").append("<div class=\"draggable\" id=\"" + ImgID + "\" style=\"max-height:100px; max-width:100px;\"><img class=\"img\"  src=\"" + path.toString() + "\"></div>");
            }
            else {
            //else inserts Text
                $("#platno").append("<div class=\"draggable\" id=\"" + ImgID + "\" style=\"max-height:100px; max-width:100px;\"><p class=\"text\">" + $("#Textbox1").val() + "</p></div>");
            }
 
I tried to get a class and a source of an image, but this doen't work:
elem[2] = $(selector.toString()).children().first().css("class"); //third attribute is class (text or image)
if ($(selector.toString()).children().first().css("class") == "img") {
    elem[3] = $(selector.toString()).children().first().css("src");  //fourth is image source
}
else {
    elem[3] = $(selector.toString()).children().first().text;   //or fourth is text of an element
}
 
Thanks for replies!
 
Pepin z Hane
Posted 21-Apr-13 7:28am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,
 
You can obtain value of class attribute using .attr('class') method. Similarly you can use .attr('src') to obtain the value of src attribute of an image. The text from the paragraph can be read using .text().
 
e.g.
$('#paraId').text();          // Will return the text of paragraph with id as paraId
$('#myImage').attr('src');    // will return the value of src attribute src
$('#myDiv').attr('class');    // Will return the value of class attribute
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Here is my solution, this is another way how to do that, but for the text in the paragraph, the only way.
 
var tmp = $(selector.toString()).children().first();
                elem[2] = tmp[0].className;  //third is class (text or image)
                //elem[2] = $(selector.toString()).children().first().attr("class"); //this works too
                if (elem[2] == "img") {
                    elem[3] = tmp[0].src;    
                    //elem[3] = $(selector.toString()).children().first().attr("src"); //fourth is image source //this works too
                }
                else {
                    elem[3] = tmp[0].innerText;   //or fourth is text of an element
                }
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 269
1 OriginalGriff 240
2 CPallini 195
3 Maciej Los 177
4 BillWoodruff 173
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 21 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100