Solved

JQuery Syntax

Posted on 2015-02-14
3
76 Views
Last Modified: 2015-02-14
I am trying to grab the Dta Value from a specific select option:
<script>
 				var usage_sel = $('select[name="item_options[usage]"]').attr('data-damage');
 				var usage_var = usage_sel.data('damage'); 		
 			
 				if ( usage_var == null  ) {
 					usage = 1;
 				} else if ( usage_var == undefined ) {
 					usage = 1;
 				} else {
 					usage = usage_var;
 				}	
 				alert(usage);	
</script>

Open in new window

The select name is item_options[usage]
the data is data-damage

What am I doing wrong?
0
Comment
Question by:rgranlund
  • 2
3 Comments
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 40610255
var usage_sel = $('select[name="item_options[usage]"]').attr('data-damage');
                         
should be

var usage_sel = $('select[name="item_options[usage]"]').data('damage');
0
 
LVL 7

Author Comment

by:rgranlund
ID: 40610287
When I do this: var usage_sel = $('select[name="item_options[usage]"]').data('damage');   I have it to alert on change and I get undefined.  Do I need to add more to it to get the DATA value from the selected option?
0
 
LVL 7

Author Closing Comment

by:rgranlund
ID: 40610293
I had to do the following: var usage_sel = $('select[name="item_options[usage]"]').find(':selected').data('damage');
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now