views:

128

answers:

4

My idea is to press a button that takes me to a web page. I've created a thing that dynamically creates a button and an anchor tag. When the button is clicked, I want it to "click"/fire up the anchor tag..

I've uploaded a demo to my site, when you try it out just leave everything as it is. Click the first button then the add button right away.. then try to click the dynamically created button.

Nothing happens, but if you watch the source you can find an anchor tag with the ID FL1000, I've set it up so that the anchor tag gets the ID from the value of the created button + 1000 just incase I need to use that ID..

Thanks guys...

edit: this is optimized for google chrome, haven't tried it out with other browser.

edit 2 (solution): Instead of "triggering" the click function on the anchor tag I simply made a variable that takes the href attribute from the anchor tag and goes to that location through document.location.

example:

var disis = $(this).val();
var tislink = '#' + disis + '1000';
var alink = $(tislink).attr('href');
window.open(alink,'_blank');

Thanks guys!

A: 

Whats the story with this:

var tislink = '"#' + disis + '1000"';

do you mean

var tislink = '#' + disis + '1000';

?

edl
Sorry, that was a last minute thing i tried, after hours of failing you try all sort of dumb stuff (if you're like me.. which isn't a good thing). Thanks
Noor
+2  A: 

if your idea is to press a button that takes someone to a web page, what is the reason you need to create links? why not just say in javascript

window.location = "your url";

and you can do this in the same function that creates your link, if you really have to have a link created on the page

And in case you HAVE TO fire a click on the anchor tag, you can do this with jQuery trigger
a nice and simple example at http://api.jquery.com/trigger/

Raine
thanks for the tip!
Noor
A: 

If you want the button to do something, you have to either add an onclick event for it or handle it server side.

Something along the lines of:

newBtn.setAttribute('onclick','window.location = "' + nyalanken + '";');

jmoreno
thanks, got the solution! check the post.
Noor
+1  A: 

You haven't told the new button what to do. For example, try this:

.attr({onclick: 'alert("hello");' type: 'button', 
    class: 'stor blocks red awesome neu', value: vID, id: 'getmylink'});

When you click, you'll get the alert, now just replace it with either `document.location="newpage.html", or what you will.

egrunin
This didn't work, i think there's something wrong with anchor tag maybe. I've tried setting up an alert to show up when i click the button and it works. Helped me get an idea though, updated the post with the solution!. +1 for help
Noor