inherit
169267
0
Nov 29, 2024 7:30:04 GMT -8
Mike
Praise God Almighty!
1,534
July 2011
riccetts
|
Post by Mike on Mar 28, 2014 13:02:01 GMT -8
How can I apply an ID to a button?
$(document).ready(function(){ $("button").click(function(){ Here's my problem, what if I have more than one button?
|
|
inherit
162752
0
Nov 7, 2024 3:58:23 GMT -8
Pebble
Where it all does or doesn't happen!
1,437
January 2011
pebbleleague
|
Post by Pebble on Mar 28, 2014 13:19:03 GMT -8
You can add the id to the html button :
<button type="button" id="buttonid">Click Me!</button> and then the jquery part becomes :
$( "#buttonid" ).click(function() { alert( "button clicked" ); });
|
|
inherit
King Oligochaete
126470
0
Feb 24, 2021 12:23:15 GMT -8
Wormopolis
Stop rating plugins with 1 star that dont deserve it.
20,002
June 2008
tbserialkillers
Wormo's Mini-Profile
|
Post by Wormopolis on Mar 28, 2014 13:46:46 GMT -8
or when you create the button, set attribute nwbtn=$('<button>').attr('id','mybutton');
|
|
inherit
169267
0
Nov 29, 2024 7:30:04 GMT -8
Mike
Praise God Almighty!
1,534
July 2011
riccetts
|
Post by Mike on Mar 28, 2014 13:54:37 GMT -8
So how would that work if I had a button <button>home</button>? How would I Identify that particular button? Could I have <button id="home"> ?
Edit:Ok pebbles that would work
Thanks guys
|
|
inherit
169267
0
Nov 29, 2024 7:30:04 GMT -8
Mike
Praise God Almighty!
1,534
July 2011
riccetts
|
Post by Mike on Mar 28, 2014 17:25:29 GMT -8
Ok now that I've got the button to work. now I need the script stop after I click so it doesn't repeat. I wonder if I could get to check whats been loaded and not reload the same text. Can somebody help? I did try end(); , return false; It seem to stop it altogether when clicking on the button.
|
|
inherit
King Oligochaete
126470
0
Feb 24, 2021 12:23:15 GMT -8
Wormopolis
Stop rating plugins with 1 star that dont deserve it.
20,002
June 2008
tbserialkillers
Wormo's Mini-Profile
|
Post by Wormopolis on Mar 28, 2014 17:59:34 GMT -8
disable the button. or hide it if you want to make sure they dont keep trying to click it.
|
|
inherit
169267
0
Nov 29, 2024 7:30:04 GMT -8
Mike
Praise God Almighty!
1,534
July 2011
riccetts
|
Post by Mike on Mar 28, 2014 19:22:16 GMT -8
I have something else in mind but since you replied I'm making a game or attempting to make a game. It's going to be like mob wars(facebook). I may have to create custom pages to make it work. The home page will be stationary so all the information on how to play will be there. I could create an interactive variable that the admin can create text on the page. The buttons are the menu of the game. The hit page would have a random generators to set variables like the hit title and the value of the hit if won. The page would generate at least three different hits at one time.It would have a button to fight and that would be like a dice roll of 12(6 and under hp against the user and 7 and up for the hp of the user). I would have the monetary system and gold shop included so the money earned or lost would be applied to the bank. If you want a look at the page: Mob rules This going to be an adventure for me.(trial and error)
|
|
inherit
King Oligochaete
126470
0
Feb 24, 2021 12:23:15 GMT -8
Wormopolis
Stop rating plugins with 1 star that dont deserve it.
20,002
June 2008
tbserialkillers
Wormo's Mini-Profile
|
Post by Wormopolis on Mar 28, 2014 20:47:45 GMT -8
an adventure indeed.
you might even finish it before I get fight club done
|
|
inherit
2671
0
May 14, 2013 14:40:03 GMT -8
Peter
🐺
10,615
February 2002
peter3
|
Post by Peter on Mar 29, 2014 4:02:34 GMT -8
1st Rule: You do not talk about FIGHT CLUB 2nd Rule: You DO NOT talk about FIGHT CLUB Good luck Mike, remember what we said in the chat, just ask away
|
|
inherit
169267
0
Nov 29, 2024 7:30:04 GMT -8
Mike
Praise God Almighty!
1,534
July 2011
riccetts
|
Post by Mike on Mar 29, 2014 7:10:48 GMT -8
Oh I will peter. I think you will finish fight club first Wormo. It's a working progress like myself.
|
|
inherit
77753
0
Jul 18, 2024 12:23:50 GMT -8
Bob
2,623
April 2006
bobbyhensley
|
Post by Bob on Mar 29, 2014 18:18:54 GMT -8
So how would that work if I had a button <button>home</button>? How would I Identify that particular button? Could I have <button id="home"> ? Edit:Ok pebbles that would work Thanks guys You may already have moved on from this, but maybe this will be useful information for you: In terms of the ID attribute:[/b] always be okay: $('#myElement') In the event of a malformed DOM, where there are multiple elements sharing the same ID, the first occurrence will be affected. Though this behavior shouldn't be relied upon. After all, it's an issue that shouldn't occur in the first place. Some proof of concept for you to play around with: <!DOCTYPE html> <html lang="en"> <head> <title>Multiple IDs Experiment</title>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script> <script>
$(document).ready(function () { $('#mydiv').css('color', '#f00'); $('#anotherDiv').css('color', '#0f0');
/** * .last() would give us the final element of an array, if one were * returned by getElementById. But because we're never going to * be given an array, this will actually reference the first occurence * of div#anotherDiv and overwrite the previous CSS rule. */ $('#anotherDiv').last().css('color', '#00f'); });
</script> </head>
<body> <div id="mydiv"> <p>This is the first DIV...</p> </div> <div class="anotherDiv"> <p>This is the second DIV...</p> </div> <div id="mydiv"> <p>This is the third DIV... Same as the first DIV.</p> </div> <div id="anotherDiv"> <p>This is the fourt DIV... Same as the second DIV.</p> </div> </body> </html> [/ul] No ID attribute exists, how do I narrow my results?[/a] But I'll provide a quick and dirty example. Let's assume the pertinent portion of the DOM looks like this: <div class="right form-element"> <button>Clear Fields</button> <button name="aname">Load Profile</button> </div>
<div class="some classes"> <button name="aname">Another Button</button> </div> We want to affect the first and last buttons. There are a lot of ways we can do this; and if these were the only two buttons in the entire DOM then it would be incredibly straight forward. But we're going to strictly play with selectors for the sake of this example: $(document).ready(function () { $('div.right.form-element > button:contains("Clear Fields")').css('color', '#f00'); $('div.some.classes > button:contains("Another Button")[name="aname"]').css('color', '#00f'); }); In both examples we see the following: div.right.form > ... div.some.classes > ... This states "A DIV element with class names 'RIGHT' and 'FORM' (or 'some' AND 'classes' in the latter) that is the PARENT element of ...". We can string as many class names together as necessary, according to however many classes have been assigned to the element in question. Lastly, the ">" character indicates this element is the PARENT of something else, that we're going to define. button:contains("Clear Fields") This is saying "any BUTTON elements that CONTAIN the text of 'Clear Fields'). So jQuery will search all BUTTON elements in the DIV(s) matching the selector we previously discussed, and look for any with the text of (or text containing) "Clear Fields". button:contains("Another Button")[name="aname"] Same idea as before, but now we're stringing an attribute selector. "any BUTTON elements that CONTAIN the text 'Another Button' with a NAME attribute equaling 'aname'." The format "[attr=value]" holds true for most every scenario. For example, when referencing an ANCHOR element (<a href="http://google.com>Google!</a>), you can use "[href='http://google.com']."[/ul] I wouldn't consider this post remotely close to a substitute for the official jQuery documentation. So I still encourage you to read through that when you have the opportunity. But it should suffice as a 30,000 foot overview.
|
|
inherit
169267
0
Nov 29, 2024 7:30:04 GMT -8
Mike
Praise God Almighty!
1,534
July 2011
riccetts
|
Post by Mike on Mar 29, 2014 18:26:52 GMT -8
Thanks BobAny information is welcome.
|
|