{"id":1183,"date":"2012-09-16T08:54:36","date_gmt":"2012-09-16T08:54:36","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=1183"},"modified":"2012-09-16T08:54:36","modified_gmt":"2012-09-16T08:54:36","slug":"jquery-set-title-of-div-image-or-any-element-when-button-is-clicked","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-set-title-of-div-image-or-any-element-when-button-is-clicked\/","title":{"rendered":"jQuery Set Title of Div, Image or any element when button is clicked"},"content":{"rendered":"<p>If you ever wondered how to set title of Div, Image or any element when button is clicked using jQuery, then this article is for you. In this article, I am going to show with examples, how easy it is to <em><strong>set the title of an image, a div or any other element using jQuery<\/strong><\/em>. Read on to find out more.<!--more--><\/p>\n<h2>Examples to Set Title of Div, Image or any element when button is clicked using jQuery<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=1183\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>Let&#8217;s assume that we have a div with ID &#8220;my_div&#8221; and we want to set title of this div to a new value and alert the old and new values, when a button is clicked. Here is how we can use <em><strong>jQuery to set div title<\/strong><\/em>:<\/p>\n<h3>Example: Full source code to set title of an element (div, image, etc.) when button is clicked using jQuery<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;jQuery Set Title of Div, Image, any element when button is clicked&lt;\/title&gt;\n&lt;script type=\"text\/javascript\" src=\"js\/jquery-1.8.2.min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n\n&lt;div id=\"my_div\" title=\"My DIV title\"&gt;This is my existing div.&lt;\/div&gt;\n\n&lt;input type=\"submit\" name=\"sbt_get_title\" id=\"sbt_get_title\" value=\"Set Div Title &amp; Get It\"&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\n\n$('#sbt_get_title').on(\"click\", function(){\n\n\tvar title_old = $('#my_div').attr('title');\n\n\t$('#my_div').attr('title', 'This is a NEW TITLE for my div.');\n\n\tvar title = $('#my_div').attr('title');\n\n\talert( 'Div Old Title is: '+  title_old + '\\n'+\n\t\t\t'Div New Title is: '+  title );\n\n});\n\n&lt;\/script&gt; \n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p>When you run the code, click on the button to instantly <strong>set the div title<\/strong> to the new one and then alert both the old and new div titles. You can simply change the selector in the jQuery code and replace it with an image&#8217;s ID or class to alert it&#8217;s corresponding title. You can apply the same procedure to set title of a div, image or any other element.<\/p>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=1183\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h5>Simple, isn&#8217;t it?<\/h5>\n<p>Do you know of any other ways to <em><strong>set title of an Image, Div or any element when button is clicked<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you ever wondered how to set title of Div, Image or any element when button is clicked using jQuery, then this article is for you. In this article, I am going to show with examples, how easy it is to set the title of an image, a div or any other element using jQuery. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":395,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,13],"tags":[40,42,41],"class_list":["post-1183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-2","tag-jquery-demos-2","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-j5","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/1183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=1183"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/1183\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=1183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=1183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=1183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}