8 Replies - 1695 Views - Last Post: 31 August 2014 - 03:38 PM

#1 14th_Doctor   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-February 11

Populating an Alert Box with an Input Attribute Value

Posted 31 August 2014 - 01:23 PM

I'm trying to figure this out, and I've tried a few ways this morning, but can't quite nail it.

Let's say I have this input code below...

<input type="submit" class="btn2" id="btn2" value="btn2" data-key1="fbi" data-key2="cia"/><br>



What I want to do with jQuery is search for both data-key elements, and return their values in an alert box. I've been looking through jQuery's API for assistance in searching attributes, but nothing seems to apply.

This is my latest past without the .click functionality. Any help figuring out how to accomplish this would be appreciated.

Thanks.

$('#data-key').text()

function check () {
    var content = 
document.getElementById("data-key").innerText;
alert(content);
}
  });
});




Is This A Good Question/Topic? 0
  • +

Replies To: Populating an Alert Box with an Input Attribute Value

#2 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3215
  • View blog
  • Posts: 12,098
  • Joined: 03-December 12

Re: Populating an Alert Box with an Input Attribute Value

Posted 31 August 2014 - 02:11 PM

document.getElementById("data-key").innerText;


This wouldn't work because the id is not 'data-key'.


You could grab the attribute, but for it to work you would need the actual attribute name, not a subset.
Was This Post Helpful? 0
  • +
  • -

#3 14th_Doctor   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-February 11

Re: Populating an Alert Box with an Input Attribute Value

Posted 31 August 2014 - 02:21 PM

You're right, I didn't even think about that, when I wrote up that variation. I need to grab the attribute, what can I use to get that?

This post has been edited by andrewsw: 31 August 2014 - 02:27 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3215
  • View blog
  • Posts: 12,098
  • Joined: 03-December 12

Re: Populating an Alert Box with an Input Attribute Value

Posted 31 August 2014 - 02:24 PM

jQuery .Attr()
SO
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Populating an Alert Box with an Input Attribute Value

Posted 31 August 2014 - 02:26 PM

jQuery data() is what jQuery uses to retrieve data-attribute values.
Was This Post Helpful? 0
  • +
  • -

#6 14th_Doctor   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-February 11

Re: Populating an Alert Box with an Input Attribute Value

Posted 31 August 2014 - 02:55 PM

View Postandrewsw, on 31 August 2014 - 02:26 PM, said:

jQuery data() is what jQuery uses to retrieve data-attribute values.


From what I'm reading on data, it looks like I'd need 2 buttons one to attach the data, and one to push the data out in the alert, or can I put both into one button?
Was This Post Helpful? 0
  • +
  • -

#7 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3215
  • View blog
  • Posts: 12,098
  • Joined: 03-December 12

Re: Populating an Alert Box with an Input Attribute Value

Posted 31 August 2014 - 03:00 PM

Look at their example,

<script>
$( "button" ).click(function() {
  var value;
 
  switch ( $( "button" ).index( this ) ) {
    case 0 :
      value = $( "div" ).data( "blah" );
      break;
    case 1 :
      $( "div" ).data( "blah", "hello" );
      value = "Stored!";
      break;



How would you add an alert to that?
Was This Post Helpful? 0
  • +
  • -

#8 14th_Doctor   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-February 11

Re: Populating an Alert Box with an Input Attribute Value

Posted 31 August 2014 - 03:24 PM

See looking at their example, they have 1 button to see the div, and 3 buttons that each either pull information or remove information from it.

I would think, it would be something akin to
$( "button" ).click(function() {
  var value;
value = $( "data-key" ).data



But I know that's not it.

This post has been edited by andrewsw: 31 August 2014 - 03:26 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#9 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3215
  • View blog
  • Posts: 12,098
  • Joined: 03-December 12

Re: Populating an Alert Box with an Input Attribute Value

Posted 31 August 2014 - 03:38 PM

Consider this modification of their example,

case 1 :
   $( "div" ).data( "blah", "hello" );
   value = "Stored!";
   alert("The value of \"blah\" is " + value);
   break;



or in your case,

<div data-key="val">A div</div>

case 1 :
   value = $( "div" ).data( "key");
   alert("The value of \"blah\" is " + value);
   break;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1