0

I want to trigger a click on a div on page load, but which div depends on the url.

E.g. HTML

<div id="1" class="clickMe"></div>
<div id="2" class="clickMe"></div>
<div id="3" class="clickMe"></div>
<div id="4" class="clickMe"></div>

A page URL might be:

http://this-site.com/pageName?thing=2

On this occasion I would want to trigger a click on div with class clickMe, with id=2

I know I can use $( ".clickMe" ).trigger( "click" ); but is there a way to select the div clickMe that I want?

StudioTime
  • 22,603
  • 38
  • 120
  • 207
  • possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript). Combine that with [this](http://stackoverflow.com/questions/5891840/how-to-use-javascript-variables-in-jquery-selectors/5891850#5891850) and you've got your answer – Phil Dec 19 '13 at 00:07
  • As in based of the URL click this element? – Michael Tempest Dec 19 '13 at 00:07
  • @Phil Hi, I know how to get URL variables into JS, that's not the question. The question is: can I choose a div to trigger a click on – StudioTime Dec 19 '13 at 00:10
  • Please read the question before voting to close based on duplication. This is not even close to a duplicate of the suggestion. – StudioTime Dec 19 '13 at 00:12
  • @DarrenSweeney And please read the suggestions given. You made no mention of being able to extract the query parameter. The other question I've linked to shows exactly how to use a variable within a jQuery selector (this one in case you missed it - http://stackoverflow.com/questions/5891840/how-to-use-javascript-variables-in-jquery-selectors). – Phil Dec 19 '13 at 00:18
  • @Phil Thanks, I didn't mention RE URL because I might not actually do it that way, it was the best way I could think of explaining what I wanted to do. Plus I can already do that. You can get flamed on here for asking two questions so I don't do it ha ha - thanks again, appreciated – StudioTime Dec 19 '13 at 00:22

2 Answers2

2

I know how to get URL variables into JS

Then store that in a variable called "thing" and

$( "#" + thing ).trigger( "click" );
fatman45
  • 68
  • 1
  • 9
1

You want to first of all get the thing variable from the query string into jQuery, then specify it as the correct ID selector to trigger a click on. I'm going to copy a function used in the article that Phil flagged this post as a duplicate of:

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

var thing_id = getParameterByName('thing');
$( ".clickMe#" + thing_id ).trigger( "click" );

You can specify a class and an ID like this: $('.className#IDname'), however ID's should always be unique in a web page, so you shouldn't actually need to do that. This should suffice:

$('#' + thing_id).trigger('click');
Community
  • 1
  • 1
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • Awesome, many thanks and +1 for the explanation - not very well documented unless I was searching incorrectly, couldn't find anything but this is priceless to me! – StudioTime Dec 19 '13 at 00:19