Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
JavaScript
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:
JavaScript
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

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.
JavaScript
$('#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
 
Share this answer
 
v2
Here is my solution, this is another way how to do that, but for the text in the paragraph, the only way.

JavaScript
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
                }
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900