0

I have a website which loads from an ajax code and I have an language select image link which I want to add an onclick event to. The onclick event has to be added to the <a> tag. When the page loads in the browser, it forms the html code as

<a class="whatever">
<div id="1234" class="whatever1" width: 25px; height: 25px;">
<img class="whatever2" alt="" src="imagelink.png" width: 25px; height: 25px;">
</div>
</a>

Since I cannot change the html code manually (I could add javascript into the page), how could I load this: onClick="SetCookie('Language','En');document.location.href='http://example.com/';" into the <a> tag ?

Could I use the class in the <a> tag to target the element and load the onclick event into it somehow?

Thanks in advance for any help.

Titanium 7
  • 68
  • 1
  • 11
  • Are you using [JQuery](http://jquery.com)? – CatDadCode Feb 24 '14 at 04:02
  • I tried using jquery just like Subash Selvaraj told me to except jquery makes my website malfunction. I guess I can't use it, I'll have to use javascript. – Titanium 7 Mar 06 '14 at 23:13
  • JQuery *is* javascript. – CatDadCode Mar 07 '14 at 01:16
  • I know that jquery is javascript but it has too much coding packed together that it messes with how my website loads. That's why I need something a lot simpler. – Titanium 7 Mar 07 '14 at 17:29
  • If JQuery is "messing with how your website loads" then I think there is probably some very bad practices happening in the javascript on your website. JQuery is entirely namespaced and you can even turn off the `$` function by calling `jQuery.noConflict()` right after jquery is loaded. – CatDadCode Mar 07 '14 at 19:27
  • Could you show me an example how to stop the jquery code interference after the code loads? Thanks for trying to help me. I appreciate it. – Titanium 7 Mar 08 '14 at 00:17
  • Is your code public? If so, link me to the repository and I'll even clone and take a look at it tonight :) – CatDadCode Mar 08 '14 at 00:43

1 Answers1

0

use Jquery. here whatever should be replaced with your <a>'s class name.

$("body").on("click",".whatever",function(){
    SetCookie('Language','En');
    document.location.href='http://example.com/';
});
Subash Selvaraj
  • 3,385
  • 1
  • 14
  • 17
  • I posted a script code in the header but it seems like it didn't work. This is what I put: `` I also tried a few suggestings from this post: (http://stackoverflow.com/questions/12284168/adding-onclick-event-dynamically-using-jquery) – Titanium 7 Feb 25 '14 at 04:26
  • have you included jquery in your code? `` or `` – Subash Selvaraj Feb 25 '14 at 05:02
  • I tried using jquery and it makes my website malfunction(and the code doesn't appear after the page loads). It would be nice if there was another solution to inject or insert the html code after the page loads. – Titanium 7 Mar 06 '14 at 23:14