Click here to Skip to main content
11,924,800 members (57,098 online)
Rate this:
Please Sign up or sign in to vote.
See more: Javascript jQuery
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
Please Sign up or sign in to vote.

Solution 1


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().

$('#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
Rate this: bad
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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151125.3 | Last Updated 21 Apr 2013
Copyright © CodeProject, 1999-2015
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