Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to change 'Home' that is within 'span' to 'Click I agree' when the user clicks the img element corresponding to 'Home'. The following is my code.

XML
<li>
            <a class=""  id=""  onclick=""><img src="homelogo.jpg" style=" cursor:pointer; width:40px; height:40px;"  /> </a>
            <span>Home</span>
        </li>
Posted
Updated 9-Jan-15 17:32pm
v2

1 solution

From your makup, doing

JavaScript
$(document).on("click", "img", function() {    
    $("span").text("Click I agree")
});

which is the same as

JavaScript
$("img").onClick(funtion() {
    $("span").text("Click I agree")
})


would work

But it would change the text on all spans when an image is clicked on a webpage.

To select the exact span with a specific image, you could specify an ID to the <a> link like so,

HTML
<li>
    <a class="" id="home" ><img src="homelogo.jpg" style=" cursor:pointer; width:40px; height:40px;" /></a>
    <span>Home</span>
</li>


Then you could do

JavaScript
$(document).on("click", "#home img", function() {      
    $("#home").next().text("Click I agree")
});


It gets easier if the span also has its own ID, you could just select that specific ID and change the text.

Read
JQuery API selectors[^]
JQuery Events[^]
JQuery .next()[^]
 
Share this answer
 

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